【问题标题】:Message: stale element reference: element is not attached to the page document消息:过时的元素引用:元素未附加到页面文档
【发布时间】:2018-10-23 08:29:48
【问题描述】:

我正在尝试自动化我们几乎每天都在执行的任务。我读到 python 结合 selenium 将是完成这项任务的完美选择。欢迎任何建议:)

在下面查看我的代码。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

usernameStr = 'USERNAME'
passwordStr = 'PASSWORD'

browser = webdriver.Chrome()
browser.get('https://www.partner.co.il/he-il/login/login/?TYPE=100663297&REALMOID=06-f94d9340-8677-4c32-9f36-efd036fe99f0&GUID=&SMAUTHREASON=0&METHOD=GET&SMAGENTNAME=vmwebcms9&TARGET=-SM-HTTPS%3a%2f%2fwww%2epartner%2eco%2eil%2fcopa%2fpages%2fprotected%2fprotectedredirect%2easpx%3foriginal%3dhttps%3a%2f%2fwww%2epartner%2eco%2eil%2faccount_actions')

# fill in username 

username = browser.find_element_by_xpath('//*[@id="USER"]')
username.send_keys(usernameStr)

# fil the password

password = browser.find_element_by_xpath('//*[@id="PASSWORD"]')
password.click()
password.send_keys(passwordStr)


# press the login button

signInButton = browser.find_element_by_id('LoginBtn')
signInButton.click()

# go to the abroad page

browser.get(('https://biz.partner.co.il/he-il/biz/international/going-abroad'))

但它会返回这个

=========== RESTART: C:\Program Files (x86)\Python36-32\login2.py ===========
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python36-32\login2.py", line 22, in <module>
    password.send_keys(passwordStr)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 479, in send_keys
    'value': keys_to_typing(value)})
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 237, 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=66.0.3359.139)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.14393 x86_64)

【问题讨论】:

    标签: python selenium selenium-webdriver selenium-chromedriver staleelementreferenceexception


    【解决方案1】:

    此错误消息...

    selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
      (Session info: chrome=66.0.3359.139)
      (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.14393 x86_64)
    

    ...暗示在为 password 字段调用 send_keys() 时,元素变成了stale

    有多个事实需要解决如下:


    密码字段

    password 字段包含 onfocus 属性包含函数 managePasswordTxt()。因此,一旦您单击 password 字段,就会调用 managePasswordTxt() JavaScript 并且您必须诱导 WebDriverWait 以使该字段可点击,您可以使用以下解决方案:

    • 代码块:

      from selenium import webdriver
      from selenium.webdriver.chrome.options import Options
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
      options = Options()
      options.add_argument("start-maximized")
      options.add_argument("disable-infobars")
      options.add_argument("--disable-extensions")
      driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\ChromeDriver\chromedriver_win32\chromedriver.exe')
      driver.get('https://www.partner.co.il/he-il/login/login/?TYPE=100663297&REALMOID=06-f94d9340-8677-4c32-9f36-efd036fe99f0&GUID=&SMAUTHREASON=0&METHOD=GET&SMAGENTNAME=vmwebcms9&TARGET=-SM-HTTPS%3a%2f%2fwww%2epartner%2eco%2eil%2fcopa%2fpages%2fprotected%2fprotectedredirect%2easpx%3foriginal%3dhttps%3a%2f%2fwww%2epartner%2eco%2eil%2faccount_actions')
      username = driver.find_element_by_xpath("//input[@id='USER']").send_keys("Alex")
      driver.find_element_by_xpath("//input[@id='PASSWORD']").click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='PASSWORD']"))).send_keys("Pruteanu")
      
    • 浏览器客户端快照:


    版本兼容性

    另一个问题是您使用的二进制文件之间的版本兼容性,如下所示:

    • 您正在使用 chromedriver=2.35
    • chromedriver=2.35 的发行说明明确提及以下内容:

    支持 Chrome v62-64

    • 您正在使用 chrome=66.0
    • ChromeDriver v2.38 的发行说明明确提及以下内容:

    支持 Chrome v65-67

    • 我们不知道您的 Selenium 客户端 版本。

    所以 ChromeDriver v2.35Chrome 浏览器 版本 v66.0 之间存在明显的不匹配

    解决方案

    • Selenium升级到当前级别Version 3.11.0
    • ChromeDriver 升级到当前的ChromeDriver v2.38 级别。
    • Chrome 版本保持在 Chrome v66.x 级别。 (as per ChromeDriver v2.38 release notes)
    • 清理你的项目工作区通过你的IDE重建你的项目只需要依赖。
    • 使用CCleaner 工具在执行测试套件之前和之后清除所有操作系统杂务。
    • 如果您的基础 Web Client 版本太旧,请通过 Revo Uninstaller 卸载它并安装最新的 GA 和发布版本的 Web Client
    • 进行系统重启
    • 执行您的@Test

    【讨论】:

    • 这不是浏览器与 chromedriver 兼容性的问题。这在 Chrome v66+ 和 Chromedriver 2.38 上失败。我已经测试了这段代码,但在 Chrome 上的 v2.36+ 上失败了。
    • 同样,我已经为这个项目切换到 Firefox 并且运行相同的代码,奇怪的事情
    【解决方案2】:

    您的定位器很好。但是,当您单击或将焦点更改为密码元素时,会出现StaleElementReference 异常。

    添加此导入语句

    from selenium.common.exceptions import StaleElementReferenceException
    

    并将其添加到您的代码中

    try:
        ActionChains(browser).send_keys(Keys.TAB).send_keys(passwordStr).perform()
        password.send_keys(passwordStr)
    except StaleElementReferenceException:
        pass
    

    使用有效的用户名和密码,代码可以正常登录。

    【讨论】:

      【解决方案3】:

      修改您的代码以填写密码,如下所示

      # fil the password
      
      password = browser.find_element_by_xpath('//*[@id="PASSWORD"]')
      password.click()
      password = browser.find_element_by_xpath('//*[@id="PASSWORD"]')
      password.send_keys(passwordStr)
      

      当您尝试对页面上不再可用的 webElement 执行操作时,会引发 StaleElement 异常,这可能是因为页面已刷新,因此您的代码保留了不再有效的对象。在对密码字段执行单击后,您需要再次进行查找,然后尝试在其上发送密钥。单击后xpath可能会发生变化,如果是这种情况,请在单击后更新您的xpath。

      【讨论】:

        【解决方案4】:

        切换到 FireFox 驱动程序解决了使用我在此处发布的相同代码的问题

        【讨论】:

          猜你喜欢
          • 2021-12-19
          • 2019-09-30
          • 2022-08-17
          • 1970-01-01
          • 2019-02-03
          • 2022-01-21
          • 2021-05-10
          • 2020-11-13
          • 1970-01-01
          相关资源
          最近更新 更多