如何单击所有产品链接、搜索元素并返回。
How to Click at all product links, search an element and navigate back.
我正在尝试一个一个地点击 "boots" 相关产品 link,点击一个产品后 link 然后执行 if-else 条件然后导航回来,做这个website。我正在尝试获取 links,例如 -By.tagName("a")。但我无法获得 links(我在 XP (74b9af1ba95c1e355e08a2172b279888) 上获得输出-chrome] -> 标签名称:a])。这是互联网上获得 link秒。但是我无法获得 links。这是我的代码:
public class GuestShoppingTestCase {
UtilityMethods util = new UtilityMethods();
@BeforeSuite
public void launchBrowser() {
UtilityMethods.openBrowser(Constants.BROWSER_NAME);
UtilityMethods.launchWebsite(Constants.URL);
}
@Test
public void PurchaseItemTest() throws InterruptedException, IOException {
Thread.sleep(5000);
try {
util.getdriver().switchTo().alert().dismiss();
} catch (Exception e) {
final By DROPDOWN = By.cssSelector("li[class='atg_store_dropDownParent']");
final By DROPDOWN_LINK = By.cssSelector("a[class='accord-header ']");
// Navigate to the Women Category through Dropdowns
List<WebElement> dropdowns = new WebDriverWait(util.getdriver(), 15)
.until(ExpectedConditions.presenceOfAllElementsLocatedBy(DROPDOWN));
WebElement women = (WebElement) dropdowns.stream()
.flatMap(dropdown -> dropdown.findElements(DROPDOWN_LINK).stream())
.filter(link -> link.getText().equals("WOMEN")).findFirst().orElse(null);
if (women != null) {
new WebDriverWait(util.getdriver(), 15).until(ExpectedConditions.elementToBeClickable(women));
Actions action = new Actions(util.getdriver());
action.moveToElement(women).build().perform();
// Search and Click a sub-category "Boots"
new WebDriverWait(util.getdriver(), 20)
.until(ExpectedConditions.elementToBeClickable(util.getdriver().findElement(By.xpath("//a[@title='Boots']"))))
.click();
// Finding all links and saving in a list------------
Thread.sleep(10000);
util.getdriver().findElement(By.id("atg_store_prodList"));
List<WebElement> alllinks = util.getdriver().findElements(By.tagName("a"));
// Printing all links-------
System.out.println(alllinks);
for (int i = 6; i < alllinks.size(); i++) {
System.out.println(alllinks.get(i));
WebElement elementToBeClicked = alllinks.get(i);
elementToBeClicked.click();
util.getdriver().findElement(By.id("atg_behavior_addItemToCart")).click();
// util.getdriver().switchTo().alert().dismiss();
if (util.getdriver().findElement(By.xpath("//a[contains(text(),'Continue Shopping')]"))
.isDisplayed()) {
util.getdriver().findElement(By.xpath("//a[contains(text(),'Continue Shopping')]")).click();
util.getdriver().navigate().back();
}
else {
util.getdriver().findElement(By.xpath("//a[@title='Checkout']")).click();
Select selectCountry = new Select(
util.getdriver().findElement(By.id("atg_store_countryNameSelect")));
selectCountry.selectByValue("US");
Thread.sleep(3000);
util.clickbyXpath(Constants.PROCEEDTOCHECKOUT);
util.getdriver().findElement(By.id("atg_store_catSubProdList"))
.sendKeys(BarneyTestData.getvalueofexcel(4, 1));
}
}
}
您的代码存在一些问题。您的代码包含以下行:
util.getdriver().findElement(By.id("atg_store_prodList"));
List<WebElement> alllinks = util.getdriver().findElements(By.tagName("a"));
而不是那个,应该是:
WebElement prodList = util.getdriver().findElement(By.id("atg_store_prodList"));
List<WebElement> alllinks = prodList.findElements(By.xpath(".//div[@class='product-name']/a"));
这将 return 包含所有产品名称的列表。你不应该使用通用代码:
列出所有链接 = util.getdriver().findElements(By.tagName("a"));
因为 HTML 源中有一些链接无法在网页上进行交互,而且您想单击产品以将其添加到购物车。
我正在尝试一个一个地点击 "boots" 相关产品 link,点击一个产品后 link 然后执行 if-else 条件然后导航回来,做这个website。我正在尝试获取 links,例如 -By.tagName("a")。但我无法获得 links(我在 XP (74b9af1ba95c1e355e08a2172b279888) 上获得输出-chrome] -> 标签名称:a])。这是互联网上获得 link秒。但是我无法获得 links。这是我的代码:
public class GuestShoppingTestCase {
UtilityMethods util = new UtilityMethods();
@BeforeSuite
public void launchBrowser() {
UtilityMethods.openBrowser(Constants.BROWSER_NAME);
UtilityMethods.launchWebsite(Constants.URL);
}
@Test
public void PurchaseItemTest() throws InterruptedException, IOException {
Thread.sleep(5000);
try {
util.getdriver().switchTo().alert().dismiss();
} catch (Exception e) {
final By DROPDOWN = By.cssSelector("li[class='atg_store_dropDownParent']");
final By DROPDOWN_LINK = By.cssSelector("a[class='accord-header ']");
// Navigate to the Women Category through Dropdowns
List<WebElement> dropdowns = new WebDriverWait(util.getdriver(), 15)
.until(ExpectedConditions.presenceOfAllElementsLocatedBy(DROPDOWN));
WebElement women = (WebElement) dropdowns.stream()
.flatMap(dropdown -> dropdown.findElements(DROPDOWN_LINK).stream())
.filter(link -> link.getText().equals("WOMEN")).findFirst().orElse(null);
if (women != null) {
new WebDriverWait(util.getdriver(), 15).until(ExpectedConditions.elementToBeClickable(women));
Actions action = new Actions(util.getdriver());
action.moveToElement(women).build().perform();
// Search and Click a sub-category "Boots"
new WebDriverWait(util.getdriver(), 20)
.until(ExpectedConditions.elementToBeClickable(util.getdriver().findElement(By.xpath("//a[@title='Boots']"))))
.click();
// Finding all links and saving in a list------------
Thread.sleep(10000);
util.getdriver().findElement(By.id("atg_store_prodList"));
List<WebElement> alllinks = util.getdriver().findElements(By.tagName("a"));
// Printing all links-------
System.out.println(alllinks);
for (int i = 6; i < alllinks.size(); i++) {
System.out.println(alllinks.get(i));
WebElement elementToBeClicked = alllinks.get(i);
elementToBeClicked.click();
util.getdriver().findElement(By.id("atg_behavior_addItemToCart")).click();
// util.getdriver().switchTo().alert().dismiss();
if (util.getdriver().findElement(By.xpath("//a[contains(text(),'Continue Shopping')]"))
.isDisplayed()) {
util.getdriver().findElement(By.xpath("//a[contains(text(),'Continue Shopping')]")).click();
util.getdriver().navigate().back();
}
else {
util.getdriver().findElement(By.xpath("//a[@title='Checkout']")).click();
Select selectCountry = new Select(
util.getdriver().findElement(By.id("atg_store_countryNameSelect")));
selectCountry.selectByValue("US");
Thread.sleep(3000);
util.clickbyXpath(Constants.PROCEEDTOCHECKOUT);
util.getdriver().findElement(By.id("atg_store_catSubProdList"))
.sendKeys(BarneyTestData.getvalueofexcel(4, 1));
}
}
}
您的代码存在一些问题。您的代码包含以下行:
util.getdriver().findElement(By.id("atg_store_prodList"));
List<WebElement> alllinks = util.getdriver().findElements(By.tagName("a"));
而不是那个,应该是:
WebElement prodList = util.getdriver().findElement(By.id("atg_store_prodList"));
List<WebElement> alllinks = prodList.findElements(By.xpath(".//div[@class='product-name']/a"));
这将 return 包含所有产品名称的列表。你不应该使用通用代码:
列出所有链接 = util.getdriver().findElements(By.tagName("a"));
因为 HTML 源中有一些链接无法在网页上进行交互,而且您想单击产品以将其添加到购物车。