簡體   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