0

I am trying to print quallity or any variable after the loop but it's not working. It's working before the loops only. How can I use it? I want to send get request with all this details to link.

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()

for x in range(1, 2):
    driver.get("http://sparo.live/movies?page=%d" % (x))
    text = driver.find_elements_by_xpath("//div[@class='boxALL']/article/a" )
    print (len(text))
    array = []
    info = []
    for link in text:
        links = link.get_attribute("href")
        array.append(links)

    for i in array:    
        driver.get(i)
        title = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[1]/h1")
        english_title = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[1]/h2")
        year = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[1]/div[2]")
        geners = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[5]/div[1]/span[1]")
        description = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[5]/div[9]/p")
        preimg = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[1]/div[1]/img")
        img = preimg.get_attribute("src")
        quallity = driver.find_element_by_xpath(".//*[@id='tab-content1']/div/div[3]/a/div[1]/p[2]")
        pretrailer = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[6]/ul/li/iframe")
        trailer = pretrailer.get_attribute("src")
        prelinks = driver.find_element_by_xpath(".//*[@id='tab-content1']/div/div[3]/a")
        watchonclick = prelinks.get_attribute("onclick")
        watchlink = watchonclick.replace('open', 'location.replace')
        driver.execute_script(watchlink)
        find_div = driver.find_elements_by_xpath("//div[@class='box']/div[2]/a" )
        for div in find_div:
            links_to_watch = div.get_attribute("href")
            break
        print ("quallity: '%s'." % quallity.text)

3
  • Surely if the code ever enters the inner loop, quallity gets printed and you are free to use it after the loop(s). If you aren't, then it's because array has length 0 every single time, which can only be due to text being similarly having length 0. Try printing out these variables and see if you find something other than what you expect. Commented Nov 1, 2017 at 18:43
  • I guess you're the big fan of for loops :) What is the point of for x in range(1, 2)? And what is the point of breaking loop after first iteration (in for div in find_div)? Commented Nov 1, 2017 at 18:50
  • @Andersson I am learning its my first script, range1,2 need to go from page X TO Y but i am debugging so its only 1 page. Commented Nov 1, 2017 at 19:09

1 Answer 1

1

You have to store the result somewhere if you want to print that outside of loop:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()

quality=[] #edited line

for x in range(1, 2):
    driver.get("http://sparo.live/movies?page=%d" % (x))
    text = driver.find_elements_by_xpath("//div[@class='boxALL']/article/a" )
    print (len(text))
    array = []
    info = []
    for link in text:
        links = link.get_attribute("href")
        array.append(links)

    for i in array:
        driver.get(i)
        title = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[1]/h1")
        english_title = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[1]/h2")
        year = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[1]/div[2]")
        geners = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[5]/div[1]/span[1]")
        description = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[5]/div[9]/p")
        preimg = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[1]/div[1]/img")
        img = preimg.get_attribute("src")
        quallity = driver.find_element_by_xpath(".//*[@id='tab-content1']/div/div[3]/a/div[1]/p[2]")
        quality.append(quallity.text)     #edited line
        pretrailer = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[6]/ul/li/iframe")
        trailer = pretrailer.get_attribute("src")
        prelinks = driver.find_element_by_xpath(".//*[@id='tab-content1']/div/div[3]/a")
        watchonclick = prelinks.get_attribute("onclick")
        watchlink = watchonclick.replace('open', 'location.replace')
        driver.execute_script(watchlink)
        find_div = driver.find_elements_by_xpath("//div[@class='box']/div[2]/a" )
        for div in find_div:
            links_to_watch = div.get_attribute("href")
            break
        for quality_type in quality:
            print(quality_type)

output:

HD
HD
HD
HD
HD
HD
HD
HD
HD
BDRip
Sign up to request clarification or add additional context in comments.

1 Comment

It's important to note that retrieving the text of the element inside the loop is what makes this work. If you instead did quality.append(quallity) you could not later do for q in qaulity:print(q.text). The reason for this is Selenium doesn't fetch the data from the page until it is requested. That means the element is no longer valid when a new page is navigated to or something else makes the DOM change. The OP's code loads a page each loop so holding the references to elements won't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.