使用selenium和python单击链接

时间:2017-04-21 17:26:33

标签: python selenium phantomjs

我尝试使用带有selenium的python脚本从此网站(https://www.tusubtitulo.com/season/26/10)下载文件

这是代码:

from selenium import webdriver

site="https://www.tusubtitulo.com/season/26/10"
driver = webdriver.PhantomJS()
el = driver.find_element_by_link_text("Descargar").click()
driver.close()

但是我收到了这个错误:

    raise exception_class(message, screen, stacktrace)

NoSuchElementException: {"errorMessage":"Unable to find element with link text 'Descargar'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"97","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:53425","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"link text\", \"value\": \"Descargar\", \"sessionId\": \"be219f20-26b5-11e7-bfb9-63e6de8a8a0a\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/be219f20-26b5-11e7-bfb9-63e6de8a8a0a/element"}}

链接查看xpath:http://imgur.com/a/kNWzC 我也尝试过使用xpath,如几个答案中所解释的那样,结果相同:

from selenium import webdriver

site="https://www.tusubtitulo.com/season/26/10"
driver = webdriver.PhantomJS()
xpath=".//*[@id='episodes']/table[1]/tbody/tr[3]/td[7]/a"
el = driver.find_element_by_xpath(xpath).click()
driver.close()

具有相同的结果。

但如果我使用" WebDriverWait"方法,我得到了一个不同的错误:

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

site="https://www.tusubtitulo.com/season/382/7"
driver = webdriver.PhantomJS()
xpath = ".//*[@id='episodes']/table[3]/tbody/tr[3]/td[7]/a"
element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, xpath))).click()
driver.close()

错误:

    raise TimeoutException(message, screen, stacktrace)

TimeoutException

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我是您所指的网站的管理员。我们的系统不允许来自脚本的请求。您需要从浏览器下载。这是我们的规则。