stuum1.
stuum1.

Reputation: 25

Selenium Python 3.4.3 Automatic Downloading (Button) Section

so i am currently struggling to be able to click the "download button". I have tried using the css_selector in multiple different ways and the way i am most sure of (below) is giving me an error for some reason. Can someone please have a look at my code below and help me understand what is wrong with the code.

Here is the whole code of the program:

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
import time

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


wait = WebDriverWait(browser, 6)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.results dl")))

link_num_1 = browser.find_element_by_css_selector('div.results dl dt a')
link_num_1.click()

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.download dl")))

link_num_2 = browser.find_elements_by_css_selector('div.download dl dt a')
link_num_2[1].click()

This is where the problem is:

Site_link_trynum_1 = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.category-detail li a")))
#Site_link_trynum_1 = browser.find_element_by_css_selector("div.li a")
Site_link_trynum_1.click()

Upvotes: 0

Views: 258

Answers (1)

sowa
sowa

Reputation: 1329

From the looks of it div.category-detail li a cannot be found because that page has been opened in a new window, while Selenium is looking for it in the original window (torrents.eu). You can try to switch windows using the switch_to_window() method, all though since you are dealing with a lot of popups, it might be better to fetch the outgoing url and open it in the current window like so,

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
import time

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


wait = WebDriverWait(browser, 6)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.results dl")))

link_num_1 = browser.find_element_by_css_selector('div.results dl dt a')
link_num_1.click()

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.download dl")))

link_num_2 = browser.find_elements_by_css_selector('div.download dl dt a')
link = link_num_2[1].get_attribute("href")
browser.get(link)

Site_link_trynum_1 = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.category-detail li a.torrent")))
#Site_link_trynum_1 = browser.find_element_by_css_selector("div.li a")
Site_link_trynum_1.click()

It might be worth considering though, if fetching links of a site is your aim, to avoid Selenium all together and write a sort of web crawler. If you do move forward with Selenium though, it might be worth to take a look at Splinter, which is an abstraction layer on top of Selenium, making it easier to write tests. The test above would look something like so when using Splinter,

from splinter import Browser

def test_torrents():

    with Browser() as browser:

        browser.visit('https://torrentz.eu/')

        # Search for something
        browser.find_by_id("thesearchbox").fill("Limitless")

        # Hit the search button
        searchButton = browser.find_by_id('thesearchbutton')
        searchButton.click()

        # Get the results and click the first
        results = browser.find_by_css('div.results dl dt a')
        results.first.click()

        # Get the torrents and visit the second (first is sponsored link)
        torrents = browser.find_by_css('div.download dl dt a')
        browser.visit(torrents[1]["href"])

        # Get the file
        torrent_file = browser.find_by_css("div.category-detail li a.torrent")
        print torrent_file["href"]

Upvotes: 1

Related Questions