【问题标题】:Why am I unable to click submit button of "Accept Cookies" popup?为什么我无法点击“Accept Cookies”弹出窗口的提交按钮?
【发布时间】:2021-12-04 17:11:33
【问题描述】:

我想从家谱网站上抓取一些数据,我需要登录但我无法点击 Selenium 的提交按钮,可能是因为我需要接受 cookie 但我也无法点击它.

代码如下:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

PATH = "/usr/local/bin/chromedriver"
driver = webdriver.Chrome(PATH)
driver.implicitly_wait(10)

driver.get("https://en.geneanet.org/connexion/")
driver.find_element_by_xpath('//*[@id="tarteaucitronPersonalize2"]').click()
driver.find_element_by_id("_username").send_keys('user')
driver.find_element_by_id ("_password").send_keys("pwd")
driver.find_element_by_id("_submit").click()

按钮如下所示:

这是与“提交”按钮对应的 HTML: 提交

cookie 弹出窗口和 HTML 如下所示:

我收到以下错误消息:

(vscrap) admin@Admins-MacBook-Pro V2 % /Users/admin/Documents/Coding/Python/Scrapping/vscrap/bin/python /Users/admin/Documents/Coding/Python/Scrapping/UpWork/V2/Selenium_login-immo.py
Traceback (most recent call last):
  File "/Users/admin/Documents/Coding/Python/Scrapping/UpWork/V2/Selenium_login-immo.py", line 15, in <module>
    driver.find_element_by_id("_submit").click()
  File "/Users/admin/Documents/Coding/Python/Scrapping/vscrap/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/Users/admin/Documents/Coding/Python/Scrapping/vscrap/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "/Users/admin/Documents/Coding/Python/Scrapping/vscrap/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Users/admin/Documents/Coding/Python/Scrapping/vscrap/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button type="submit" id="_submit" name="_submit" class="button-register no-margin-bottom large">...</button> is not clickable at point (429, 324). Other element would receive the click: <div id="tarteaucitronRoot" class="tarteaucitronBeforeVisible">...</div>
  (Session info: chrome=95.0.4638.69)

(vscrap) admin@Admins-MacBook-Pro V2 % /Users/admin/Documents/Coding/Python/Scrapping/vscrap/bin/python /Users/admin/Documents/Coding/Python/Scrapping/UpWork/V2/Selenium_login-immo.py
Traceback (most recent call last):
  File "/Users/admin/Documents/Coding/Python/Scrapping/UpWork/V2/Selenium_login-immo.py", line 15, in <module>
    driver.find_element_by_id("_submit").click()
  File "/Users/admin/Documents/Coding/Python/Scrapping/vscrap/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/Users/admin/Documents/Coding/Python/Scrapping/vscrap/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "/Users/admin/Documents/Coding/Python/Scrapping/vscrap/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Users/admin/Documents/Coding/Python/Scrapping/vscrap/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button type="submit" id="_submit" name="_submit" class="button-register no-margin-bottom large">...</button> is not clickable at point (429, 324). Other element would receive the click: <div id="tarteaucitronRoot" class="tarteaucitronBeforeVisible">...</div>
  (Session info: chrome=95.0.4638.69)

为什么我不能点击这个提交按钮?

【问题讨论】:

    标签: selenium selenium-webdriver xpath css-selectors webdriverwait


    【解决方案1】:

    OK,accept all按钮是一个动态元素,所以理想情况下点击你需要诱导WebDriverWaitelement_to_be_clickable()的元素,你可以使用以下@987654323之一@:

    • 使用CSS_SELECTOR

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.tarteaucitronCTAButton.tarteaucitronAllow#tarteaucitronPersonalize2"))).click()
      
    • 使用XPATH

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='tarteaucitronCTAButton tarteaucitronAllow' and @id='tarteaucitronPersonalize2']"))).click()
      
    • 注意:您必须添加以下导入:

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

    【讨论】:

    • 谢谢,我已经添加了缺少的导入和 webDriverWait,但仍然存在相同的 cookie 弹出窗口并且未点击提交。
    • 我给它添加了一个短暂的延迟,然后它就起作用了。 w_item = WebDriverWait(driver,50).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.tarteaucitronCTAButton.tarteaucitronAllow#tarteaucitronPersonalize2"))) time.sleep(1) w_item.click()
    猜你喜欢
    • 1970-01-01
    • 2020-02-26
    • 2014-12-12
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    相关资源
    最近更新 更多