1

我想点击一个<a>标签,它看起来像一个我可以用 selenium 内置函数点击的元素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)
4

1 回答 1

1

此错误消息...

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 横幅,您必须先关闭横幅。

一旦您关闭横幅以识别您需要诱导WebDriverWait的可点击元素element_to_be_clickable(),您可以使用以下任一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
    
于 2020-09-15T10:46:46.297 回答