繁体   English   中英

提取特定类硒Webdriver下/中的所有链接(java)

[英]fetch all links under/in a specific class-selenium webdriver (java)

有没有一种方法可以提取特定类下的所有链接?

问题是,我正在编写一个测试,要求我单击随机的item/product但是如果通过By.tagName("a")创建所有链接的列表,它将获取页面上的所有链接。 更确切地说,考虑一下该网站 ,现在我想随机从pretsummer saleaccessoriesbt lawn'16salelookbook或者在单击summer sale ,我想随机单击其下的一种产品。 知道怎么做吗?

这是我的程序的片段:

HTTPS

如果要从您提到的站点中选择所有类,请在xpath下使用:

List<WebElement> allMenus = driver.findElements(By.xpath(".//a[contains(@class, 'level0')]"));

然后循环遍历WebElements以获得所需的菜单项。 此外,可以观察到,将鼠标悬停在特定项目上之后,子菜单项就会显示出来。 要执行鼠标悬停操作,我们必须使用Actions类。 请找到以下代码以供参考。

Actions mouseHovers = new Actions(driver);
// Looping through the menu items stored in the above list variable
for(WebElement eachMenu : allMenus) {
    mouseHovers.moveToElement(eachMenu).perform();
    // Select the desired sub-menu by using the above line of code by replacing the "eachMenu" element with the respective sub-menu element.
}

希望这可以帮助。

实际上,您使用不正确的xpath来定位pretsummer saleaccessoriesbt lawn'16salelookbook ,链接尝试如下:

List<WebElement> allLinks = driver.findElements(By.cssSelector("a.level0"));
Random random = new Random();
WebElement randomLink = allLinks.get(random.nextInt(allLinks.size()));
randomLink.click();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM