【问题标题】:Python3 Selenium on Ubuntu script works when visible fails when headlessUbuntu脚本上的Python3 Selenium在无头时可见失败时工作
【发布时间】:2021-03-28 13:02:26
【问题描述】:

我在 Ubuntu 上成功运行了一个 python3 selenium 脚本,当我尝试无头运行时,它在元素不可交互时出错:

Traceback (most recent call last):
  File "file.py", line 143, in <module>
    driver.find_element_by_xpath('//*[@id="ctl00_MainContent_MyReportViewer_ctl04_ctl00"]').click()                  
  File "/home/jobs/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click                      
    self._execute(Command.CLICK_ELEMENT)
  File "/home/jobs/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute                      
    return self._parent.execute(command, params)
  File "/home/jobs/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute                      
    self.error_handler.check_response(response)
  File "/home/jobs/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response                      
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable                      
  (Session info: headless chrome=87.0.4280.88

我看过一篇文章,说这可能是 chrome 与对象交互的方式不同,所以我在 Firefox 上也尝试过,结果相同。

我要更改的唯一行是:

chrome_options.add_argument("--headless")

【问题讨论】:

    标签: python-3.x selenium xpath css-selectors webdriverwait


    【解决方案1】:

    此错误消息...

    selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
    

    ...暗示当您调用所需的WebElement 时,它是不可交互的。

    该元素似乎是一个动态元素,所以点击该元素需要诱导WebDriverWaitelement_to_be_clickable(),可以使用以下Locator Strategies之一:

    • 使用CSS_SELECTOR

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#ctl00_MainContent_MyReportViewer_ctl04_ctl00"))).click()
      
    • 使用XPATH

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id="ctl00_MainContent_MyReportViewer_ctl04_ctl00"]"))).click()
      
    • 注意:您必须添加以下导入:

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

    更新

    在您仍然无法单击元素时,您可以使用 ActionChains,如下所示:

    • 使用CSS_SELECTOR

      element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#ctl00_MainContent_MyReportViewer_ctl04_ctl00")))
      ActionChains(driver).move_to_element(element).click(element).perform()
      
    • 使用XPATH:

      element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id="ctl00_MainContent_MyReportViewer_ctl04_ctl00"]")))
      ActionChains(driver).move_to_element(element).click(element).perform()
      
    • 注意:您必须添加以下导入:

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

    【讨论】:

    • 我尝试将 WebDriverWait 设置为 20,然后设置为 30,但没有成功。我在运行过程中截取了屏幕截图以查看发生了什么,我认为 Selenium 可能看不到全屏。不过,这可能是一个红鲱鱼。我附上了上面的截图。
    • @StephenYorke 查看更新后的答案并告诉我状态。
    猜你喜欢
    • 2014-01-07
    • 1970-01-01
    • 2014-07-15
    • 2017-05-28
    • 2017-06-03
    • 2019-05-07
    • 2019-03-22
    • 2017-02-06
    • 1970-01-01
    相关资源
    最近更新 更多