[英]How to Click at all product links, search an element and navigate back.
我試圖一個一個地單擊與“ boots”相關的產品鏈接,單擊產品鏈接后,然后執行if-else條件,然后瀏覽回去,在此網站上進行 。 我正在嘗試獲取-By.tagName(“ a”)之類的鏈接。 但是我無法獲得鏈接(我在XP(74b9af1ba95c1e355e08a2172b279888)上獲得了輸出鑲邊(chrome-output chrome)]->標簽名稱: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));
}
}
}
您的代碼存在一些問題。 您的代碼具有下面提到的行:
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"));
這將返回包含所有產品名稱的列表。 您不應該使用通用代碼:
列出所有鏈接= util.getdriver()。findElements(By.tagName(“ a”));
因為HTML源中有一些鏈接在網頁上不可交互,並且您也想單擊產品以將其添加到購物車中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.