gpt4 book ai didi

python - Selenium:列表索引超出范围错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:11 25 4
gpt4 key购买 nike

我得到了

Traceback (most recent call last):
File "<stdin>", line 2, in <module>
IndexError: list index out of range

每当我尝试运行此代码时出错。我只是想打印此页面上的所有 URL。有人可以告诉我,我做错了什么吗??

from selenium import webdriver
browser = webdriver.Firefox()
browser.get("http://www.tour-india.net/best-of-india.htm")
cities=browser.find_elements_by_css_selector(".posts1>a>h2")
for i in range(0,len(cities)):
cities1=browser.find_elements_by_css_selector(".posts1>a>h2")[i]
cities1.click()
title=browser.find_elements_by_xpath("//title")
content=browser.find_elements_by_css_selector(".tours_text_innerpage.content_margin_top")
currentUrl=browser.current_url
print currentUrl
browser.back()

编辑: 我正在对代码进行一些修改我在 for 循环之后再次添加了 cities=browser.find_elements_by_css_selector(".posts1>a>h2") ,突然索引错误停止了。现在我很困惑为什么会这样。??

from selenium import webdriver
browser = webdriver.Firefox()
browser.get("http://www.tour-india.net/best-of-india.htm")
cities=browser.find_elements_by_css_selector(".posts1>a>h2")
for i in range(0,len(cities)):
cities=browser.find_elements_by_css_selector(".posts1>a>h2")
cities1=browser.find_elements_by_css_selector(".posts1>a>h2")[i]
cities1.click()
title=browser.find_elements_by_xpath("//title")
content=browser.find_elements_by_css_selector(".tours_text_innerpage.content_margin_top")
currentUrl=browser.current_url
print currentUrl
browser.back()

编辑:我的整个回溯

>>> import traceback
>>> from selenium import webdriver
>>> browser = webdriver.Firefox()
>>> browser.get("http://www.tour-india.net/best-of-india.htm")
>>> cities=browser.find_elements_by_css_selector(".posts1>a>h2")
>>> for i in range(0,len(cities)):
... try:
... #cities=browser.find_elements_by_css_selector(".posts1>a>h2")
... cities1=browser.find_elements_by_css_selector(".posts1>a>h2")[i]
... cities1.click()
... title=browser.find_elements_by_xpath("//title")
... content=browser.find_elements_by_css_selector(".tours_text_innerpage.content_margin_top")
... currentUrl=browser.current_url
... print currentUrl
... browser.back()
... except:
... print traceback.format_exc()
...
http://www.tour-india.net/golden-triangle.htm
http://www.tour-india.net/golden-triangle-varanasi.htm
http://www.tour-india.net/magnificent-rajasthan.htm
http://www.tour-india.net/northindia-rajasthan-tour.htm
http://www.tour-india.net/north_india_himalaya_tour.htm
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
IndexError: list index out of range

http://www.tour-india.net/southindia-panorma.htm
http://www.tour-india.net/classical-rajasthan-tours.htm
http://www.tour-india.net/rajasthan-forts.htm
http://www.tour-india.net/india-nepal-tour.htm
http://www.tour-india.net/southindia-glimpses.htm
http://www.tour-india.net/enchanting-southindia.htm
http://www.tour-india.net/shekhawati-tours.htm
http://www.tour-india.net/delhi-tour.htm
http://www.tour-india.net/bombay-goa.htm
http://www.tour-india.net/royal-rajasthan.htm
http://www.tour-india.net/grand-mughal.htm
http://www.tour-india.net/north_india_himalaya_tour.htm
http://www.tour-india.net/northindia-images.htm
http://www.tour-india.net/karnataka-heritage.htm
http://www.tour-india.net/leh-ladakh.htm
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
IndexError: list index out of range

http://www.tour-india.net/darjeeling-sikkim.htm
http://www.tour-india.net/himalayan-heritage.htm
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
IndexError: list index out of range

http://www.tour-india.net/rajasthan-goa.htm
http://www.tour-india.net/rajasthan-forts-palaces.htm
http://www.tour-india.net/rajasthan-mp.htm
http://www.tour-india.net/rajasthan-nepal.htm
http://www.tour-india.net/splendid-gujarat.htm

最佳答案

在 cities 解决问题后再次调用 cities 变量。我仍然不知道为什么。但它工作正常。因为没有人发布答案。接受我自己的回答

from selenium import webdriver
browser = webdriver.Firefox()
browser.get("http://www.tour-india.net/best-of-india.htm")
cities=browser.find_elements_by_css_selector(".posts1>a>h2")
for i in range(0,len(cities)):
cities=browser.find_elements_by_css_selector(".posts1>a>h2")
cities1=browser.find_elements_by_css_selector(".posts1>a>h2")[i]
cities1.click()
title=browser.find_elements_by_xpath("//title")
content=browser.find_elements_by_css_selector(".tours_text_innerpage.content_margin_top")
currentUrl=browser.current_url
print currentUrl
browser.back(

关于python - Selenium:列表索引超出范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12570329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com