python selenium消息:过时的元素参考:元素未附加到页面文档

时间:2019-03-02 09:13:34

标签: python selenium

因此,我正在编写此浏览器游戏机器人(仅用于学习硒),并且遇到以下问题:我需要单击“登录”按钮,然后再切换选项卡并单击另一个“登录”按钮。到现在,元素已更改(并且我得到消息:stale element reference:元素未附加到页面文档  错误),并且我不知道该如何刷新它们。这里的代码:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    char database[50];
    int n = 0, b;
    string line;
    ifstream data("/Users/MY-PC/Desktop/path.txt");
    //while (!data.eof())
    while (n!= sizeof(line))
    {
        if (line == " " || line == ",")
        {
            cout << "FAIL" << endl;
        }
        else
        {
            getline(data, (line));
            cout << line[n] << endl;
            n = n + 1;
        }
    }
    system("pause");
    return 0;
}

我试图制作另一个for循环,以刷新“链接列表”,但是它不起作用。

已经感谢!

更新:整个错误

    linklist = browser.find_elements_by_xpath("//span[contains(text(), 'Spielen')]")

            for element in linklist:
                handles = browser.window_handles
                time.sleep(3)

                for tab in handles:
                    time.sleep(3)
                    browser.switch_to.window(tab)
                    time.sleep(3)

                    if browser.current_url == \
                                'https://lobby.ogame.gameforge.com/de_DE/accounts':
                        element.click()
                        break

编辑:我理解为什么我得到了错误,但是我不知道“解决方法”是什么(我昨天整天都在阅读线程;)。 > 只需再次使用命令Traceback (most recent call last): File "C:/Users/leosc/PycharmProjects/ogameBot/inGame.py", line 3, in <module> logInFile.logIn() File "C:\Users\leosc\PycharmProjects\ogameBot\logInFile.py", line 87, in logIn element.click() File "C:\Users\leosc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click self._execute(Command.CLICK_ELEMENT) File "C:\Users\leosc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute return self._parent.execute(command, params) File "C:\Users\leosc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\Users\leosc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document (Session info: chrome=72.0.3626.119) (Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.17134 x86_64)并不能解决问题,这就是大多数线程建议的错误。
那么,有没有“变通”建议?

0 个答案:

没有答案