Search code examples
pythonseleniumbittorrentutorrent

Selenium Python 3.4.3 Automatic Torrenting: Link section


What i am trying to do is make a simple program that lets me run and it basically goes to Torrentz and follows a few link to finally be able to download the file through uttorent. Below is what i have coded so far and i cant seem to make the variable linkElem work. And i also cant seem to make linkElem.find_elements_by_xpath go to the link necessary. If you think you know what is wrong, please do help. Thanks.

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

Solution

  • I don't think you can and should rely on the href attribute value. Instead, get the links from under the dl elements inside the search results container. Also, add a wait:

    # 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 in your case would contain all of the search results links, links[0] is the first link.