![](/img/trans.png)
[英]How to fetch all links and click those links one by one with Selenium WebDriver
[英]How to fetch all links and click those links one by one using Selenium WebDriver
我正在使用Selenium WebDriver和java。
我從網頁上獲取所有鏈接,並嘗試逐個點擊每個鏈接。 我收到以下錯誤:
錯誤org.openqa.selenium.StaleElementReferenceException:在緩存中找不到元素 - 自查詢以來頁面可能已更改命令持續時間或超時:30.01秒有關此錯誤的文檔,請訪問: http : //seleniumhq.org/ exceptions / stale_element_reference.html構建信息:版本:'2.25.0',修訂版:'17482',時間:'2012-07-18 21:09:54'
這是我的代碼:
public void getLinks()throws Exception{
try {
List<WebElement> links = driver.findElements(By.tagName("a"));
int linkcount = links.size();
System.out.println(links.size());
for (WebElement myElement : links){
String link = myElement.getText();
System.out.println(link);
System.out.println(myElement);
if (link !=""){
myElement.click();
Thread.sleep(2000);
System.out.println("third");
}
//Thread.sleep(5000);
}
}catch (Exception e){
System.out.println("error "+e);
}
}
實際上,它在輸出中顯示
[[FirefoxDriver:XP上的firefox(ce0da229-f77b-4fb8-b017-df517845fa78)] - >標簽名稱:a]
作為鏈接,我想消除這些形式的結果。
有以下場景沒有這么好的主意:
for (WebElement element : webDriver.findElements(locator.getBy())){
element.click();
}
為什么? 因為無法保證element.click();
對其他找到的元素沒有影響,因此DOM
可能會被更改,因此StaleElementReferenceException
也是StaleElementReferenceException
。
最好使用以下方案:
int numberOfElementsFound = getNumberOfElementsFound(locator);
for (int pos = 0; pos < numberOfElementsFound; pos++) {
getElementWithIndex(locator, pos).click();
}
這樣更好,因為您將始終刷新WebElement
,即使之前的點擊也會對其產生一些影響。
編輯:添加示例
public int getNumberOfElementsFound(By by) {
return webDriver.findElements(by).size();
}
public WebElement getElementWithIndex(By by, int pos) {
return webDriver.findElements(by).get(pos);
}
希望足夠。
信用歸於“貸款”。
我也得到“陳舊的例外”所以我使用'貸款'答案並且完美地工作。 如果有人需要知道如何從結果頁面點擊每個鏈接試試這個(java)
clickAllHyperLinksByTagName("h3");
其中“h3”標簽包含超鏈接
public static void clickAllHyperLinksByTagName(String tagName){
int numberOfElementsFound = getNumberOfElementsFound(By.tagName(tagName));
System.out.println(numberOfElementsFound);
for (int pos = 0; pos < numberOfElementsFound; pos++) {
getElementWithIndex(By.tagName(tagName), pos).click();
driver.navigate().back();
}
}
public static int getNumberOfElementsFound(By by) {
return driver.findElements(by).size();
}
public static WebElement getElementWithIndex(By by, int pos) {
return driver.findElements(by).get(pos);
}
WebDriver _driver = new InternetExplorerDriver();
_driver.navigate().to("http://www.google.co.in/");
List <WebElement> alllinks = _driver.findElements(By.tagName("a"));
for(int i=0;i<alllinks.size();i++)
System.out.println(alllinks.get(i).getText());
for(int i=0;i<alllinks.size();i++){
alllinks.get(i).click();
_driver.navigate().back();
}
如果您可以使用WebDriver.get()
而不是WebElement.click()
來測試鏈接,則另一種方法是將每個找到的WebElement
的href
值保存在單獨的列表中。 這樣就可以避免StaleElementReferenceException
因為在使用第一個WebElement.click()
導航之后,您不會嘗試重用后續的WebElement
。
基本示例:
List<String> hrefs = new ArrayList<String>();
List<WebElement> anchors = driver.findElements(By.tagName("a"));
for ( WebElement anchor : anchors ) {
hrefs.add(anchor.getAttribute("href"));
}
for ( String href : hrefs ) {
driver.get(href);
}
//extract the link texts of each link element
for (WebElement elements : linkElements) {
linkTexts[i] = elements.getText();
i++;
}
//test each link
for (String t : linkTexts) {
driver.findElement(By.linkText(t)).click();
if (driver.getTitle().equals(notWorkingUrlTitle )) {
System.out.println("\"" + t + "\""
+ " is not working.");
} else {
System.out.println("\"" + t + "\""
+ " is working.");
}
driver.navigate().back();
}
driver.quit();
}
完整說明閱讀此POST
List <WebElement> links = driver.findElements(By.tagName("a"));
int linkCount=links.size();
System.out.println("Total number of page on the webpage:"+ linkCount);
String[] texts=new String[linkCount];
int t=0;
for (WebElement text:links){
texts[t]=text.getText();//extract text from link and put in Array
//System.out.println(texts[t]);
t++;
}
for (String clicks:texts) {
driver.findElement(By.linkText(clicks)).click();
if (driver.getTitle().equals("notWorkingUrlTitle" )) {
System.out.println("\"" + t + "\""
+ " is not working.");
} else {
System.out.println("\"" + t + "\""
+ " is working.");
}
driver.navigate().back();
}
driver.quit();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.