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

时间:2012-09-24 17:59:12

标签: python selenium

我正在

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&gt; a&gt; 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

4 个答案:

答案 0 :(得分:1)

那么,您点击每个链接,打印并返回?这非常低效。您可以使用.get_attribute方法快速获取页面上所有链接的URL。

links = [i.get_attribute('href') for i in driver.find_elements_by_xpath('.//a')]
for i in links:
    print i

将打印页面上所有链接的列表。要选择较小的页面区域,请找到要从中选择的“框架”元素,然后使用

frame.find_elements_by_xpath('//a') 

代替。

答案 1 :(得分:0)

使用len(cities)-1len返回比Python看到的列表长度多1个。

答案 2 :(得分:0)

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

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(

答案 3 :(得分:-3)

for i in range(len(cities)):

范围只有一个参数:)

你可以修改你的循环:

for city in cities:
    city.click()
    # rest is the same 

这更像是“pythonic”