我一直在尝试使用Python和Selenium从某个网站上刮一些价格我写的脚本看起来工作得很好,但有时我会在代码的这一部分得到一个错误:
#Select "Date de début"
select = Select(driver.find_element("xpath",'//*[@id="startDateSet"]'))
options = select.options
option = options[i]
select.select_by_visible_text(option.text)
print (option.text)
ListDateEfPrincipal3Semaines.append(option.text)
Error
我不明白为什么有时会发生这种情况,通常它应该只得到选项列表(其中包含至少10个元素,然后提取这些信息,但在这里似乎返回的列表只包含1个元素。
下面是前面提到的Select类:Select Class Image
下面是完整的代码:
print ("\nDébut du Programme EF Principal pour 3 semaines !")
ListPriceEfPrincipal3Semaines = []
ListDateEfPrincipal3Semaines = []
for i in range(1, 10):
print("\n")
#add options to the Chrome tab
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(executable_path=path, options=options)
driver.implicitly_wait(10) #wait max 5 seconds for the cookie page to appear, if it finds it sooner it continues
driver.get('https://www.ef.fr/ils/book-now/')
driver.find_element("xpath",'//*[@id="onetrust-accept-btn-handler"]').click()
#Select "Destination"
select = Select(driver.find_element("xpath",'//*[@id="destination"]'))
select.select_by_visible_text('Tokyo')
#Select "Formule de cours"
select = Select(driver.find_element("xpath",'//*[@id="courseSet"]'))
select.select_by_visible_text('Programme EF Principal')
#Select "Durée du séjour"
select = Select(driver.find_element("xpath",'//*[@id="durationSet"]'))
select.select_by_visible_text('3 Semaines')
#Select "Date de début"
select = Select(driver.find_element("xpath",'//*[@id="startDateSet"]'))
options = select.options
option = options[i]
select.select_by_visible_text(option.text)
print (option.text)
ListDateEfPrincipal3Semaines.append(option.text)
#Click on "Suivant"
driver.find_element("xpath",'//*[@id="btn-next"]').click()
#Pick the final price
content = driver.find_element("class name",'Summary_priceInfo__Wadbp')
#content = driver.find_element("xpath",'//div[@class="Summary_priceInfo__Wadbp"]')
FinalPrice = content.text
print("Prix final : ")
print(FinalPrice)
ListPriceEfPrincipal3Semaines.append(FinalPrice)
driver.close()
print ("\nFin du Programme EF Principal pour 3 semaines !\n")
print ("\nDébut du Programme EF estival pour 3 semaines !")
ListPriceEfEstival3Semaines = []
ListDateEfEstival3Semaines = []
for i in range(1, 10):
print("\n")
#add options to the Chrome tab
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(executable_path=path, options=options)
driver.implicitly_wait(10) #wait max 5 seconds for the cookie page to appear, if it finds it sooner it continues
driver.get('https://www.ef.fr/ils/book-now/')
driver.find_element("xpath",'//*[@id="onetrust-accept-btn-handler"]').click()
#Select "Destination"
select = Select(driver.find_element("xpath",'//*[@id="destination"]'))
select.select_by_visible_text('Tokyo')
#Select "Formule de cours"
select = Select(driver.find_element("xpath",'//*[@id="courseSet"]'))
select.select_by_visible_text('Programme EF estival')
#Select "Durée du séjour"
select = Select(driver.find_element("xpath",'//*[@id="durationSet"]'))
select.select_by_visible_text('3 Semaines')
#Select "Date de début"
select = Select(driver.find_element("xpath",'//*[@id="startDateSet"]'))
options = select.options
option = options[i]
select.select_by_visible_text(option.text)
print (option.text)
ListDateEfEstival3Semaines.append(option.text)
#Click on "Suivant"
driver.find_element("xpath",'//*[@id="btn-next"]').click()
#Pick the final price
content = driver.find_element("class name",'Summary_priceInfo__Wadbp')
#content = driver.find_element("xpath",'//div[@class="Summary_priceInfo__Wadbp"]')
FinalPrice = content.text
print("Prix final : ")
print(FinalPrice)
ListPriceEfEstival3Semaines.append(FinalPrice)
driver.close()
print ("\nFin du Programme EF estival pour 3 semaines !\n")
这是我得到的结果
runfile('D:/EFPrice_Automate.py', wdir='D:')
Début du Programme EF Principal pour 3 semaines !
d:\efprice_automate.py:15: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(executable_path=path, options=options)
Traceback (most recent call last):
File "D:\anaconda3\lib\site-packages\spyder_kernels\py3compat.py", line 356, in compat_exec
exec(code, globals, locals)
File "d:\efprice_automate.py", line 36, in <module>
option = options[i]
IndexError: list index out of range
1条答案
按热度按时间5vf7fwbs1#
此问题可能是由于在您尝试从下拉菜单中选择选项之前页面未完全加载。要解决此问题,您可以尝试使用WebDriverWait来使用显式等待,它将等待下拉选项可用,然后再尝试选择一个选项。以下是一个示例:
这将等待最多10秒,以便下拉选项变为可用。如果选项在此时间内未变为可用,则将引发TimeoutException。