【问题标题】:Selenium Java code to print the first five results from google searchSelenium Java 代码打印来自 google 搜索的前五个结果
【发布时间】:2020-10-08 12:50:17
【问题描述】:

线程“main”中的异常 org.openqa.selenium.StaleElementReferenceException: 过时的元素引用:元素未附加到页面文档 (会话信息:chrome=83.0.4103.106) 有关此错误的文档,请访问:https://www.seleniumhq.org/exceptions/stale_element_reference.html 构建信息:版本:'3.141.59',修订:'e82be7d358',时间:'2018-11-14T08:25:53' 驱动程序信息:org.openqa.selenium.chrome.ChromeDriver 功能 {acceptInsecureCerts: false, browserName: chrome, browserVersion: 83.0.4103.106, chrome: {chromedriverVersion: 83.0.4103.39 (ccbf011cb2d2b..., userDataDir: C:\Users\MOHAMM~1\AppData\L...}, goog :chromeOptions: {debuggerAddress: localhost:53846}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, 超时: {implicit: 0,pageLoad:300000,脚本:30000},unhandledPromptBehavior:关闭并通知,webauthn:virtualAuthenticators:true} 会话 ID:f9fd73fa86d306099bad2835337c25c0 *** 元素信息:{Using=xpath, value=//h3[@class='LC20lb DKV0Md']}

Code snap shot

结果显示在第一页,但是当我点击第二页结果时,我得到了 StaleElementReferenceException

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class NavigateToGoogle {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.google.com");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        //driver.findElement(By.xpath("//input[@class='gLFyf gsfi'])")).sendKeys("zahid");

        WebElement element=driver.findElement(By.name("q"));
        element.sendKeys("mohammed zahid");
        //element.click();
        Thread.sleep(3000);
        //driver.findElement(By.xpath("(//input[@type='submit'])[3]")).click();
        List<WebElement> button=driver.findElements(By.name("btnK"));
        System.out.println(button.size());
        WebElement submit=driver.findElement(By.name("btnK"));
        submit.submit();

        WebElement matchingRes=driver.findElement(By.xpath("//h3[@class='LC20lb DKV0Md']"));
        List<WebElement> listRes=matchingRes.findElements(By.xpath("//h3[@class='LC20lb DKV0Md']"));
        System.out.println(listRes.size());
        for(WebElement results:listRes) 
        {
            String value = results.getText();
            System.out.println(value);
        }

        driver.findElement(By.xpath("//a[@aria-label='Page 2']")).click();
        WebDriverWait wait = new WebDriverWait(driver, 5);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h3[@class='LC20lb DKV0Md']")));
        WebElement matchingRes1=driver.findElement(By.xpath("//h3[@class='LC20lb DKV0Md']")); 
        matchingRes1.getSize();
        List<WebElement> listRes1=matchingRes.findElements(By.xpath("//h3[@class='LC20lb DKV0Md']")); 
        System.out.println(listRes1.size());

        for(WebElement results1:listRes1)
        {
            String value = results1.getText();
            System.out.println(value);
        }
    }

}

【问题讨论】:

标签: java selenium selenium-webdriver


【解决方案1】:

我们无法对 google 结果页面元素进行硬编码,因为每个 google 页面每天都在更新。这里我提供了带有示例代码的结果搜索,你可以试试这个(我正在启动 google 页面并输入“测试”并显示所有结果并从列表中单击“测试类型”,如果您需要所有结果,您可以打印)

 public static void main(String[] args) throws Exception {
    System.setProperty("webdriver.chrome.driver", "C:\\Users\\User\\Downloads\\Chromedriver\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("https://www.google.com/");
    driver.findElement(By.name("q")).sendKeys("Testing");
    Thread.sleep(Long.parseLong("1000"));
    List<WebElement> LIST = driver.findElements(By.xpath("//ul[@role='listbox']//li/descendant::div[@class='sbl1']"));
    System.out.println(LIST.size());


    for (int i = 0; i < LIST.size(); i++)
    {
        //System.out.println(LIST.get(i).getText());
        if (LIST.get(i).getText().contains("testing types"))
        {
            LIST.get(i).click();
            break;
        }
    }
}

}

【讨论】:

    猜你喜欢
    • 2018-09-21
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2011-09-26
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    相关资源
    最近更新 更多