无法使用Selinium Webdriver单击Span Link

时间:2019-05-31 14:03:00

标签: python selenium

无法使用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块,但没有成功。

1 个答案:

答案 0 :(得分:0)

要在带有 text 作为 Magnet text 链接上click(),您需要点击{ <a>节点中的{1}}节点,您必须诱使 WebDriverWait 使元素可点击,并且您可以使用以下{{3 }}:

  • 使用<span>

    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()