Java +Selenium 框架获取元素时报 StaleElementReferenceException: stale element reference: stale element not found - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
tiRolin
V2EX    Java

Java +Selenium 框架获取元素时报 StaleElementReferenceException: stale element reference: stale element not found

  •  
  •   tiRolin 2023-07-12 14:08:14 +08:00 1595 次点击
    这是一个创建于 896 天前的主题,其中的信息可能已经有所发展或是发生改变。
    div class="cell">

    我想实现这个网站的爬虫自动登录功能: https://www.cnhnb.com/hangqing/cdlist-0-0-0-0-0-1/

    我的自动登录逻辑是先进入该网站,点击登录选项之后点击密码选项,输入账号密码同意协议并点击登录,暂时写了下面的代码

    public static void main(String[] args) { System.getProperties().setProperty("webdriver.chrome.driver","D:\\pachong\\new\\chromedriver.exe"); ChromeOptions optiOns= new ChromeOptions(); options.addArguments("--remote-allow-origins=*"); ChromeDriver chromeDriver = new ChromeDriver(options); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } Stirng url = "https://www.cnhnb.com/hangqing/cdlist-0-0-0-0-0-1/"; chromeDriver.get(url); //获取登录选项并点击该选项 WebElement loginButton = chromeDriver.findElement(By.xpath("//*[@id=\"__layout\"]/div/div/div[1]/div[1]/div/div[1]/div[2]/div[1]")); loginButton.click(); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } //获取登录窗口并切换到该窗口 WebElement element = chromeDriver.findElement(By.id("eye-iframe-sso-login")); chromeDriver.switchTo().frame(element); //获取密码登录选项并点击,该行发生 Bug WebElement pwdLogin = element.findElement(By.xpath("//*[@id=\"__layout\"]/div/div/div/div[1]/div[1]/div[3]")); pwdLogin.click(); //此处省略更多业务处理 chromeDriver.switchTo().defaultContent(); } 

    现在的问题是,每次我的代码运行到获取密码登录选项的时候就报 Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: stale element not found

    百度说是要重新获取元素,我重新获取前一个元素也没用,照样报这个错误,ChromeDriver 版本按说是没问题的,因为之前用这个框架做了一些其他爬虫都是可以正常启动并爬取信息的,最多就是控制台报了警告: Unable to find an exact match for CDP version 114, so returning the closest version found: 110 ,这个应该问题不大

    我搞了好就,不管是问 GPT 还是查百度都找不到好的解决方法,我实在没法了所以我来问问各位,有没有懂得指导指导可好啊

    6 条回复    2023-07-12 18:00:02 +08:00
    gg1025
        1
    gg1025  
       2023-07-12 14:32:40 +08:00
    第一个问题
    ```java
    Stirng url = "https://www.cnhnb.com/hangqing/cdlist-0-0-0-0-0-1/";
    ```
    这里 String 拼写错误
    gg1025
        2
    gg1025  
       2023-07-12 14:34:23 +08:00
    一个可行的方案
    ```java
    public static void main(String[] args) {
    System.getProperties().setProperty("webdriver.chrome.driver","D:\\pachong\\new\\chromedriver.exe");
    ChromeOptions optiOns= new ChromeOptions();
    options.addArguments("--remote-allow-origins=*");
    ChromeDriver chromeDriver = new ChromeDriver(options);
    try {
    Thread.sleep(3000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    String url = "https://www.cnhnb.com/hangqing/cdlist-0-0-0-0-0-1/";
    chromeDriver.get(url);
    //获取登录选项并点击该选项
    WebElement loginButton = chromeDriver.findElement(By.xpath("//*[@id=\"__layout\"]/div/div/div[1]/div[1]/div/div[1]/div[2]/div[1]"));
    loginButton.click();
    try {
    Thread.sleep(3000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    //获取登录窗口并切换到该窗口
    WebElement element = chromeDriver.findElement(By.id("eye-iframe-sso-login"));
    chromeDriver.switchTo().frame(element);
    //获取密码登录选项并点击,该行发生 Bug
    // WebElement pwdLogin = element.findElement(By.xpath("//*[@id=\"__layout\"]/div/div/div/div[1]/div[1]/div[3]"));
    // 尝试这个方案
    WebElement tabs = webDriver.findElement(By.className("tabs"));
    WebElement pwdLogin = tabs.findElements(By.className("tab-item")).get(2);
    pwdLogin.click();
    //此处省略更多业务处理
    chromeDriver.switchTo().defaultContent();
    }
    ```
    kanchi240
        3
    kanchi240  
       2023-07-12 14:37:48 +08:00
    WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frame));
    tiRolin
        4
    tiRolin  
    OP
       2023-07-12 16:37:34 +08:00
    @gg1025 太感谢你了,这个确实解决了我的问题。虽然我不知道为什么,但是现在我还是先拿来主义,以后再搞明白个中原理
    Belmode
        5
    Belmode  
       2023-07-12 17:59:33 +08:00
    https://mjj.today/i/WTT8pI

    因为登录按钮处用的 xpath 不正确
    Belmode
        6
    Belmode  
       2023-07-12 18:00:02 +08:00
    ![d83193f01191c7cb71bf762769549f62.png]( https://i3.mjj.rip/2023/07/12/d83193f01191c7cb71bf762769549f62.png)
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     914 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 20:20 PVG 04:20 LAX 12:20 JFK 15:20
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86