首页 > 解决方案 > Selenium WebDriver 等待不适用于 cookie 策略窗口的预期条件

问题描述

我试图摆脱 selenium 中的 cookie 策略窗口,该窗口弹出在我试图访问的网页的最前面。即使使用 . 尝试关闭它也是不可能的Expected conditions。我已经尝试过element_to_be_clickableand presence_of_element_located,但它们都不起作用。

奇怪的是,当我在 10 秒后执行关闭窗口的命令时,它起作用了。但是为什么WebDriver wait在这种情况下命令不起作用?另一个奇怪的事情是,当 selenium 选项卡打开时,我没有得到这个 cookie 策略窗口,只有当它减少时。

最后,根据我所阅读presence_of_element_located的内容,在网页中查找元素的“最长”预期条件。所以我在这一点上很受阻。

我得到的错误是:

Traceback (most recent call last):
  File "<input>", line 51, in <module>
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element <a id="profilelogin" href="https://accounts.marketwatch.com/login?ifr=1&amp;target=https%3A%2F%2Fwww.marketwatch.com%2Fwatchlist">...</a> is not clickable at point (38, 16). Other element would receive the click: <div id="cx-notification-wrapper" class="gdpr-message">...</div>
  (Session info: chrome=74.0.3729.131)
  (Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 4.18.0-17-generic x86_64)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/lib/python3.6/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 56, in <module>
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element <a id="profilelogin" href="https://accounts.marketwatch.com/login?ifr=1&amp;target=https%3A%2F%2Fwww.marketwatch.com%2Fwatchlist">...</a> is not clickable at point (38, 16). Other element would receive the click: <div id="cx-notification-wrapper" class="gdpr-message">...</div>
  (Session info: chrome=74.0.3729.131)
  (Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 4.18.0-17-generic x86_64)

所以我的问题是:1)为什么我只有在选项卡打开时才能获得这个 cookie 策略窗口 2)为什么预期的条件和等待语句不起作用 3)(最终)我怎样才能让我的代码正常工作?

任何帮助将不胜感激。非常感谢你

我的代码如下:

driver.get("https://www.marketwatch.com/watchlist")

WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.CLASS_NAME, "gdpr-close")))
driver.find_element_by_class_name('gdpr-close').click()

WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.ID, "wl-scrim-start")))
driver.find_element_by_id('wl-scrim-start').click()

WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.CLASS_NAME, "gdpr-close")))
driver.find_element_by_class_name('gdpr-close').click()

WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.ID, "profilelogin")))
driver.find_element_by_id('profilelogin').click()

和:

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

标签: pythonseleniumbrowserconditional-statements

解决方案


您正在等待 cookie 策略关闭按钮两次。您的代码第一次关闭 cookie 策略面板,因此它会一直等待按钮再次可点击,但在您关闭它后将无法点击。这就是为什么你得到例外。

尝试以下操作:

driver.get("https://www.marketwatch.com/watchlist")

wait = WebDriverWait(driver,30)

wait.until(EC.element_to_be_clickable((By.CLASS_NAME,"gdpr-close"))).click()

wait.until(EC.element_to_be_clickable((By.ID, "wl-scrim-start"))).click()

wait.until(EC.presence_of_element_located((By.ID, "profilelogin"))).click()

它会毫无例外地带您进入登录页面。


推荐阅读