简体   繁体   English

硒python无法获取第一个元素

[英]Can't get the first element with selenium python

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

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. 我在您的代码中注意到了一些额外的事项:-不要使用time.sleep(5)是不可靠的。 Use Webdriver waits and expected conditions to wait for elements to be present: 使用Webdriver等待和预期条件等待元素出现:

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

  • Do not use Xpaths, its also unreliable. 不要使用Xpaths,它也不可靠。 Use ID's if possible or class names 如果可能,使用ID或类名
  • Surface your exceptions, you are doing: except: pass that way you wont know when your code fails 浮出水面,您正在做的异常: except: pass这种方式,您将不知道代码何时失败
  • Organize your code in a Test Suite approach, maybe consider using Unittest2 for python 以测试套件的方法来组织代码,也许可以考虑将Unittest2用于python

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM