我想從頁面中選擇一個隨機鏈接。我添加了一個警報來查看元素是否存在。本頁面給了「被顯示」的提示,但我對日食得到以下錯誤:元素當前不可見,所以可能不會使用Java在Selenium WebDriver中隨機選擇鏈接
這裏進行交互的代碼:
String tag = "a";
boolean tagChk = driver.findElement(By.tagName(tag)).isDisplayed();
if(tagChk == true){
JOptionPane.showMessageDialog(null, tag +" is displayed");
System.out.println(tag +" is displayed");
List<WebElement> links = driver.findElements(By.tagName(tag));
WebElement randomElement = links.get(r.nextInt(links.size()));
//JOptionPane.showMessageDialog(null, "Random link clicked");
randomElement.click();
}
else
JOptionPane.showMessageDialog(null, tag +" is not displayed");
我已經檢查了這個問題的最有名的答案,他們都沒有工作。
你做一個布爾檢查'driver.findElement(By.tagName(標籤))',這是'links.get完全不同(r.nextInt(links.size()))'因此,粘貼或告訴'鏈接'中的內容會很棒! –
鏈接是WebElement列表名稱。請參閱IF語句中的第三行代碼 – Alam