java - 使用下一步按钮导航谷歌搜索页面并通过 WebDriver 打印链接标题

标签 java selenium selenium-webdriver webdriver

我试图逐页打印搜索结果的所有标题。

搜索google然后打印第一页结果的标题是成功的。但是,当我尝试单击下一步按钮然后打印第二页结果的标题时,出现了 StaleElementReferenceException: Element not found in the cache 错误。

如何在第一页上单击下一步按钮后打印第二页标题。

我的代码:

public class goopick {

public static void main(String[] args) {

    WebDriver driver = new FirefoxDriver();

    searchGoogle(driver, "Selenium Interview Questions");       

    printSiteNames(getSiteNames(driver));       

    clickNextButton(driver);

    printSiteNames(getSiteNames(driver));       

}

private static List<WebElement> getSiteNames(WebDriver driver){
    return driver.findElements(By.xpath("//*[@id='rso']/div/div/div[@class='rc']/h3/a"));
}

private static void searchGoogle(WebDriver driver, String searchString) {
    driver.get("https://www.google.co.in/?gfe_rd=cr&ei=LSX1VvXDLKmumQW7irJw&gws_rd=ssl");
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.findElement(By.id("lst-ib")).sendKeys(searchString);
    driver.findElement(By.className("lsb")).click();
}

private static void printSiteNames(List<WebElement> sitenames) {
    int i = 1;
    System.out.println("----------------------------------xxxx----------------------------");
    for (WebElement sitename : sitenames) {
        String site = sitename.getText();
        System.out.println(i + ") " + site);
        i++;
    }
    System.out.println("----------------------------------xxxx----------------------------");
}

private static void clickNextButton(WebDriver driver) {
    WebElement NextButton = driver.findElement(By.xpath("//a[@id='pnnext']/span[2]"));
    NextButton.click();
    System.out.println("Next Button Clicked");
    driver.navigate().forward();
  }
}

输出和错误:

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
Command duration or timeout: 30.79 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html

最佳答案

@praveen kumar - 您可以在到达下一页后更新您的代码。确定您要打印的文本的 xpath 并在以下代码中更新您的 xpath/class id/link id,它将为您工作:

@Test(priority=2)
        public void printPageHeader() throws InterruptedException{

            java.util.List<WebElement> allText = driver.findElements(By.xpath("//*[@id='app']/div/header/nav[1]/div/div/span/span"));
            for ( WebElement element: allText) { 
                System.out.println(element.getText());
        }  
        }

关于java - 使用下一步按钮导航谷歌搜索页面并通过 WebDriver 打印链接标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36259244/

相关文章:

javascript - 组织用于不同目的(外观、jQuery 和 Selenium)的 HTML 类名和 ID

python - 使用 Selenium 在 Python 中抓取由 javascript 注入(inject)的图像

java - 通过 Java 和 Selenium 启动 Internet Explorer 时出现 "OsProcess checkForError : CreateProcess error=193, %1 is not a valid Win32 application"

java - 形状相交

java - 无法执行目标 org.apache.maven.plugins :maven-antrun-plugin

java - Selenium:网页弹出/警报/通知消息的元素不可见异常

python - 使用 Selenium 玩 2048

angularjs - 使用npm Protractor webdriver-manager在其他端口启动Selenium服务器

java - 读取多张表 - CSV - 使用 Java

java - 替换测试中的静态变量