我想做的是制作一个简单的程序,让我运行,它基本上进入Torrentz,并遵循一些链接,最终能够通过uttorent下载文件。以下是我迄今为止编码的内容,我似乎无法使变量linkElem
工作。而且我似乎也不能让linkElem.find_elements_by_xpath
去必要的链接。如果你认为你知道哪里出了问题,请帮忙。谢谢
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
browser = webdriver.Firefox()
browser.get('https://torrentz.eu/')
searchElem = browser.find_element_by_id('thesearchbox')
searchElem.send_keys('Limitless')
searchButton = browser.find_element_by_id('thesearchbutton')
searchButton.click()
linkElem = linkElem.find_elements_by_xpath("//div[@class='results']//a[@href='/9ea0c575520a3065d85b285c9474231192368db7']")
#wait = WebDriverWait(browser, 6)
#linkElem = wait.until(EC.visibility_of_element_located((By.href, "/9ea0c575520a3065d85b285c9474231192368db7")))
#linkElem.clear()
#linkElem = browser.find_element_by_link_text('S01E20 HDTV x264 LOL ettv')
#linkElem.click()
#SignIn = browser.find_elements_by_id('signIn')
#SignIn.click()
#passwordElem.submit()
我认为您不能也不应该依赖href
属性值。相反,从搜索结果容器内的dl
元素下获取链接。此外,添加一个等待:
# wait for search results to appear
wait = WebDriverWait(browser, 6)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.results dl")))
links = driver.find_elements_by_css_selector("div.results dl dt a")
links[0].click()
在您的情况下,links
将包含所有搜索结果链接,links[0]
是第一个链接。