简体   繁体   English

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

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

Is there a way to fetch all the links under a specific class? 有没有一种方法可以提取特定类下的所有链接?

The thing is, iI am writing a test that requires me to click on a random item/product but if a create a list of all the links through By.tagName("a") , It'll fetch ALL the links on the page. 问题是,我正在编写一个测试,要求我单击随机的item/product但是如果通过By.tagName("a")创建所有链接的列表,它将获取页面上的所有链接。 To be more exact, consider this website , Now I want to randomly choose from pret , summer sale , accessories , bt lawn'16 , sale , lookbook or after clicking on summer sale , I want to randomly click on one of the products under it. 更确切地说,考虑一下该网站 ,现在我想随机从pretsummer saleaccessoriesbt lawn'16salelookbook或者在单击summer sale ,我想随机单击其下的一种产品。 any idea how to do it? 知道怎么做吗?

here is a snippet of my program : 这是我的程序的片段:

HTTPS

If you want to select all the classes from the site you mentioned, please use below xpath: 如果要从您提到的站点中选择所有类,请在xpath下使用:

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

Then loop through the WebElements to get to the desired menu item. 然后循环遍历WebElements以获得所需的菜单项。 Also, it is observed that the sub-menu items are getting displayed after the mouse is hovered on a particular item. 此外,可以观察到,将鼠标悬停在特定项目上之后,子菜单项就会显示出来。 To perform the mouse hover operation we have to use Actions class. 要执行鼠标悬停操作,我们必须使用Actions类。 Please find the below code for your reference. 请找到以下代码以供参考。

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.
}

Hope this helps. 希望这可以帮助。

Actually you are using incorrect xpath to locating pret , summer sale , accessories , bt lawn'16 , sale , lookbook , links try as below :- 实际上,您使用不正确的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.

相关问题 如何使用Selenium WebDriver逐个获取所有链接并单击这些链接 - How to fetch all links and click those links one by one using Selenium WebDriver 如何使用Selenium WebDriver来获取所有链接并单击一个链接 - How to fetch all links and click those links one by one with Selenium WebDriver 无法使用Java和Selenium Webdriver单击网页上的所有链接 - Not able to click all links on a web page using Java and Selenium Webdriver 如何在selenium webdriver中从网页中提取所有链接后单击特定链接 - How to click on specific link after extracting all the links from a web page in selenium webdriver Selenium Webdriver代码查找 <li> 下课 - Selenium Webdriver code to find <li> under class 如何验证特定文本下的文本 <DIV> 使用Selenium WebDriver(java)和Chrome浏览器标记? - How can I verify presence of text under a specific <DIV> tag using Selenium WebDriver (java) and Chrome? Selenium WebDriver和Java Robot类 - Selenium WebDriver and Java Robot Class Java Selenium XPath-将所有元素都放在特定元素下 - Java selenium xpath - getting all elements under a specific element 如何通过selenium webdriver访问链接特定部分的链接 - How to access links in the specific section of links through selenium webdriver 在主链接,Selenium Webdriver下填充链接时出现问题 - Trouble in populating the Links under a Main Link ,Selenium Webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM