Python 如何识别selenium中的可点击元素

Python 如何识别selenium中的可点击元素,python,selenium,xpath,webdriverwait,expected-condition,Python,Selenium,Xpath,Webdriverwait,Expected Condition,我想点击一个 当我尝试时,我得到的只是一个错误: DevTools listening on ws://127.0.0.1:56906/devtools/browser/a65af20c-af35-4f09-8390-abce557b8b87 Traceback (most recent call last): File "c:/Users/hugom/OneDrive/Documents/python web scraping tut/csv tutorials/freelan

我想点击一个

当我尝试时,我得到的只是一个错误:

DevTools listening on ws://127.0.0.1:56906/devtools/browser/a65af20c-af35-4f09-8390-abce557b8b87
Traceback (most recent call last):
  File "c:/Users/hugom/OneDrive/Documents/python web scraping tut/csv tutorials/freelancer.py", line 79, in <module>
    a_tag.click()
  File "C:\Users\hugom\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\hugom\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\hugom\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\hugom\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.ElementClickInterceptedException: Message: element click intercepted: Element <a class="open" data-path="/public/employees/118883/description.json" href="javascript:;">...</a> is not clickable at point (458, 1242). Other element would receive the click: <img alt="テレビCM放映中!" src="https://cw-assets.crowdworks.jp/assets/banners/fixedfooter-20200104-2000x200-5e5bc753cfe393c01c5797c1647b4fd732631e477d4308cc653f4d994f541200.png" width="1000" height="100">
  (Session info: chrome=85.0.4183.102)
DevTools在ws://127.0.0.1:56906/DevTools/browser/a65af20c-af35-4f09-8390-abce557b8b87上侦听
回溯(最近一次呼叫最后一次):
文件“c:/Users/hugom/OneDrive/Documents/pythonwebscrapingtut/csv tutorials/freegorer.py”,第79行,在
a_标记。单击()
文件“C:\Users\hugom\AppData\Local\Programs\Python\Python37-32\lib\site packages\selenium\webdriver\remote\webelement.py”,第80行,单击
self.\u执行(命令。单击\u元素)
文件“C:\Users\hugom\AppData\Local\Programs\Python\Python37-32\lib\site packages\selenium\webdriver\remote\webelement.py”,第633行,在_execute中
返回self.\u parent.execute(命令,参数)
文件“C:\Users\hugom\AppData\Local\Programs\Python\Python37-32\lib\site packages\selenium\webdriver\remote\webdriver.py”,第321行,在execute中
self.error\u handler.check\u响应(响应)
文件“C:\Users\hugom\AppData\Local\Programs\Python\Python37-32\lib\site packages\selenium\webdriver\remote\errorhandler.py”,第242行,在check\u响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.element ClickInterceptedException:消息:元素click intercepted:元素在点(4581242)处不可单击。其他元素将收到单击:
(会话信息:chrome=85.0.4183.102)
此错误消息

ElementClickInterceptedException: Message: element click intercepted: Element <a class="open" data-path="/public/employees/118883/description.json" href="javascript:;">...</a> is not clickable at point (458, 1242). Other element would receive the click: <img alt="テレビCM放映中!" src="https://cw-assets.crowdworks.jp/assets/banners/fixedfooter-20200104-2000x200-5e5bc753cfe393c01c5797c1647b4fd732631e477d4308cc653f4d994f541200.png" width="1000" height="100">
障碍元素是一个横幅元素,可能是cookies横幅,您必须首先解除该横幅

一旦您关闭横幅以识别可点击元素,您需要将
元素归纳为可点击()
,您可以使用以下任一选项:

  • 使用
    XPATH

    clickable_element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='open' and starts-with(@data-path, '/public/employees')]//font[.//font[contains(., 'Läs mer')]]")))
    
  • 注意:您必须添加以下导入:

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

我相信OP想知道他如何提前判断
点击
呼叫是否成功。@DebanjanB我如何撤销横幅?这是否回答了您的问题?
clickable_element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='open' and starts-with(@data-path, '/public/employees')]//font[.//font[contains(., 'Läs mer')]]")))
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC