Reputation: 354
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()
Upvotes: 0
Views: 1221
Reputation: 256
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
except: pass
that way you wont know when your code failsHope it helps!
Upvotes: 1