如何识别硒中的可点击元素

问题描述

我想单击一个<a>标记,它看起来像是我可以使用硒内置函数click()单击的元素。我想知道是否有人可以解释如何确定这样的元素在将来的硒编码中是否可点击。谢谢

HTML:

<a class="open" data-path="/public/employees/767772/description.json" href="javascript:;" style="display: inline;">
                <span class="icon-caret-right"></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> Läs mer
</font></font></a>

当我尝试所有操作时,我得到一个错误

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)

解决方法

此错误消息...

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">

......意味着所需元素的click()被另一个元素阻塞:

<img alt="テレビCM放映中!" src="https://cw-assets.crowdworks.jp/assets/banners/fixedfooter-20200104-2000x200-5e5bc753cfe393c01c5797c1647b4fd732631e477d4308cc653f4d994f541200.png" width="1000" height="100">

阻碍元素是横幅元素,可能是cookie横幅,您必须先关闭该横幅。

一旦解开横幅以标识可点击元素,就需要为element_to_be_clickable()引入WebDriverWait,并且可以使用以下Locator Strategies中的任何一个:

  • 使用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
    

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...