【问题标题】:Can't Click on Span Link using Selinium Webdriver无法使用 Selenium Webdriver 点击垃圾邮件链接
【发布时间】:2019-10-17 04:35:30
【问题描述】:

使用 Selinium Webdriver 无法点击下载链接

我正在尝试使用 python 自动化我每周的动漫路线

from selenium import Webdriver
import time


dr = webdriver.Chrome()
time.sleep(3)
dr.get('https://horriblesubs.info/shows/one-punch-man-s2')
time.sleep(1)
link=dr.find_element_by_class_name("rls-info-container").click()
time.sleep(3)
blink=dr.find_element_by_xpath("//span[@class='dl_type hs-magnet-link']").click()
time.sleep(6)
dr.quit()

这是我得到的错误

Traceback (most recent call last):
  File "C:\Users\Hey\AppData\Local\Programs\Python\Python37-32\Scripts\get-ShieldHero.py", line 16, in <module>
    blink=dr.find_element_by_xpath("//span[@class='dl_type hs-magnet-link']").click()
  File "C:\Users\Hey\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Users\Hey\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\Hey\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\Hey\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.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[@class='dl_type hs-magnet-link']"}
  (Session info: chrome=74.0.3729.169)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.17134 x86_64)

我想点击480链接,但是上面的代码我不能点击

我尝试过检查 html 块的其他变体,但没有成功。

【问题讨论】:

  • 用相关的 HTML 更新问题
  • 这是dl-type 不是dl_type。你为什么不为你的第二个查找器也使用一个简单的 CSS 选择器呢?就像"".dl-type.hs-magnet-link"" 一样简单。
  • 是那个错字,谢谢。

标签: python selenium xpath webdriverwait xpath-1.0


【解决方案1】:

click() 上的链接 textMagnet 项目 One Punch Man S2 您需要点击 &lt;a&gt; &lt;span&gt; 节点内的节点,您必须诱导 WebDriverWait 才能使 元素可点击,您可以使用以下任一 Locator Strategies

  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'One Punch Man S2')]//following::span[@class='dl-type hs-magnet-link']/a"))).click()
    
  • 注意:您必须添加以下导入:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 2019-07-10
    • 2012-08-15
    • 2013-02-06
    相关资源
    最近更新 更多