我正在尝试打印显示在Google搜索中的前5页链接。但是却收到StateElementReferenceException不知道哪一个出错了。
public class GoogleCoInTest { static WebDriver driver = null; public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.gecko.driver", "D:\\bala back up\\personel\\selenium\\Jars\\Drivers\\geckodriver.exe"); driver=new FirefoxDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://www.google.co.in/"); //driver.findElement(By.xpath("//input[class='gsfi']")).sendKeys("Banduchode");; WebElement search=driver.findElement(By.cssSelector("input#lst-ib")); search.sendKeys("Banduchode"); search.sendKeys(Keys.ENTER); printLinksName(); List<WebElement> fiveLinks=driver.findElements(By.xpath(".//*[@id='nav']/tbody/tr/td/a")); for(int i=0;i<5;i++){ System.out.println(fiveLinks.get(i).getText()); fiveLinks.get(i).click(); Thread.sleep(5000); printLinksName(); } } public static void printLinksName() throws InterruptedException{ List<WebElement> allLinks=driver.findElements(By.xpath("//*[@id='rso']/div/div/div/div/div/h3/a")); System.out.println(allLinks.size()); //print all list for(int i=0;i<allLinks.size();i++){ System.out.println("Sno"+(i+1)+":"+allLinks.get(i).getText()); } } }
它可以打印到第二页,但是在我得到之后
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: The element reference of <a class="fl"> stale: either the element is no longer attached to the DOM or the page has been refreshed For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
有两件事:
printLinksName()
Generic List
WebElement
for()
Page 2
List<WebElement> fiveLinks
StaleElementReferenceException
一个避免的简单解决方案 StaleElementReferenceException 是List<WebElement> fiveLinks=driver.findElements(By.xpath(".//*[@id='nav']/tbody/tr/td/a"));在for()循环中移动代码行。因此,您的代码块将如下所示:
List<WebElement> fiveLinks=driver.findElements(By.xpath(".//*[@id='nav']/tbody/tr/td/a"));
import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Q44970712_stale { static WebDriver driver = null; public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); driver=new FirefoxDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://www.google.co.in/"); //driver.findElement(By.xpath("//input[class='gsfi']")).sendKeys("Banduchode");; WebElement search=driver.findElement(By.cssSelector("input#lst-ib")); search.sendKeys("Banduchode"); search.sendKeys(Keys.ENTER); printLinksName(); for(int i=0;i<5;i++) { List<WebElement> fiveLinks=driver.findElements(By.xpath(".//*[@id='nav']/tbody/tr/td/a")); System.out.println(fiveLinks.get(i).getText()); fiveLinks.get(i).click(); Thread.sleep(5000); printLinksName(); } } public static void printLinksName() throws InterruptedException { List<WebElement> allLinks=driver.findElements(By.xpath("//*[@id='rso']/div/div/div/div/div/h3/a")); System.out.println(allLinks.size()); //print all list for(int i=0;i<allLinks.size();i++) { System.out.println("Sno"+(i+1)+":"+allLinks.get(i).getText()); } } }
注意:在此简单的解决方案中,当您完成第二页的打印时,下一次将List<WebElement> fiveLinks通过xpathwith 创建.//*[@id='nav']/tbody/tr/td/a时, Page 1 是第一个存储在fiveLinks列表中的元素。因此,您可能会再次重定向到 Page 1 。为避免这种情况,您可以考虑xpath使用适当的索引帮助。
xpath
.//*[@id='nav']/tbody/tr/td/a
Page 1
fiveLinks