我正在尝试一一单击“靴子”相关产品链接,单击产品链接后,执行 if-else 条件,然后导航回来,在此网站上进行操作。我正在尝试获取 -By.tagName("a") 之类的链接。但我无法获得链接(我在 XP 上获得 output-chrome (74b9af1ba95c1e355e08a2172b279888)] -> 标签名称:a])。这是互联网上获取链接的唯一方法。但我无法获得链接。这是我的代码:
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));
}
}
}