java - 如何单击所有产品链接、搜索元素并返回。
问题描述
我正在尝试一一单击“靴子”相关产品链接,单击产品链接后,执行 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));
}
}
}
解决方案
您的代码存在一些问题。您的代码具有以下提到的行:
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 源代码中有一些链接在网页上不可交互,并且您还想单击产品以将它们添加到您的购物车。
推荐阅读
- python - django 使用 ajax 从 api 获取数据
- download - BrowserComponent "onDownloadStart" event
- c++ - 当 boost 自动测试用例名称中包含逗号时,boost 会抱怨:“测试设置错误:没有匹配过滤器的测试用例或所有测试用例都被禁用”
- c# - 如何在单元测试期间跳过 Fluent 验证静态功能?
- jsf - Primefaces selectManyCheckbox在列中从上到下布局
- eslint - cmd eslint 找不到 IDE 发现的 ts 错误
- c# - Xamarin.IOS 上的动态约束以填充整个屏幕
- asp.net - 方法在没有调用的情况下被调用
- serialization - Django-rest:使用 2 个外键发送 POST 请求
- objective-c - 如何在不泄漏内存的情况下正确关闭 FFmpeg 流和 AVFormatContext?