0

I need to choose (open) every movie in the list, starting by first one to last, once in one, go back and open the next movie in the list until last movie. But i got a problem because the code selects the last movie and opens it not the first.

I don't know how to select the first one and repeat the process to every movie in the list.

This is the code:

from selenium import webdriver
import time

URL = 'https://www.cineplanet.cl/peliculas'
XPATH_VER_MAS_PELICULAS = '//button'
ClASS_CARTELERA = 'movie-info-details'
XPATH_COMPRAR = '//div[@class="movie-info-details"]/div[@class="movie-info-details--wrapper"]/div[@class="movie-info-details--first-button-wrapper"]/button'
PATH = 'C:\\Program Files\\chrome-driver\\chromedriver.exe'

driver = webdriver.Chrome(PATH)
driver.get(URL)

def available_movies():
    try:
        select = driver.find_element_by_class_name(ClASS_CARTELERA)
        allOptions = select.find_elements_by_xpath(XPATH_COMPRAR)
        for option in allOptions:
            option.click()
    except:
        pass

print (driver.title)
time.sleep(5)

while True:
    try:
        if XPATH_VER_MAS_PELICULAS:
            driver.find_elements_by_xpath(XPATH_VER_MAS_PELICULAS)[-1].click()
            time.sleep(5)
        else:
            break
    except:
        break

available_movies()
time.sleep(2)
driver.quit()
3
  • you will probably need to use a for-loop after gathering the length of CLASS_CARTELERA Commented May 30, 2018 at 15:53
  • I tried this stackoverflow.com/a/31349955/9492737 but it's the same result, chooses the last movie in the list. I put "print(len(like))" and it says 0 Commented May 30, 2018 at 16:51
  • It is printing 6 when I gather the length. Make sure to wait for the page to load: WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, ClASS_CARTELERA))) Commented May 30, 2018 at 16:59

1 Answer 1

1

Your code needs a lot of refactoring, but lets give it a try:

    # Keep doing this while we have movies to click
    while len(driver.find_element_by_class_name('movies-list--large-item')):
        # This will iterate through each movie
        for movie in driver.find_element_by_class_name('movies-list--large-item'):
            # Now get the button to see the movie details
            movie.find_element_by_class_name('cineplanet-icon_more').click()
            # Once you are in the movie details view do an assertion
            # Go back in the browser to the previous page
            driver.back()

        # If you reach this it means that you clicked all the movies in the current view
        # So click the View more movies button to go to the next page
        driver.find_element_by_css_selector('.movies-list--view-more-button-wrapper .call-to-action--text')

A couple of extra things that I noticed in your code: - Do not use time.sleep(5) is unreliable. Use Webdriver waits and expected conditions to wait for elements to be present:

http://selenium-python.readthedocs.io/waits.html

  • Do not use Xpaths, its also unreliable. Use ID's if possible or class names
  • Surface your exceptions, you are doing: except: pass that way you wont know when your code fails
  • Organize your code in a Test Suite approach, maybe consider using Unittest2 for python

Hope it helps!

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

6 Comments

Thanks, I'm testing the code, when I get what I want, I'm gonna order it. Let me try with your advices
The cmd shows this error: Traceback (most recent call last): File "test.py", line 60, in <module> while len(driver.find_element_by_class_name('movies-list--large-item')): TypeError: object of type 'WebElement' has no len()
Missed an s its supposed to be: while len(driver.find_elements_by_class_name('movies-list--large-item')): I didnt test the code but it should give you a good idea on what you need to do
Yes your comment was very helpful thanks, I realized few secs later, the "s" was missed, and add it, but the problem right now is: selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document I tried with driver.implicitly_wait and time.sleep and when it is going back shows that error
Yes I figured that would be the case. That is happening because when you retrieve an element through selenium it does it from a snapshot of the page DOM, when you come navigate to another page and come back the DOM has changed, so when you try to click an element that you previously retrieved it raises that exception. You need to keep a list of the movies that you already clicked (maybe by the movie name) and when you retrieve all the available movies, if you already click it you can skip it.
|

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.