【问题标题】:I've got IndexError: list index out of range after selecting an element with xpath我有 IndexError: list index out of range after select an element with xpath
【发布时间】:2018-03-16 00:03:47
【问题描述】:

我使用 xpath 和 web 驱动程序为 2 个不同的链接制作了一个 python 程序。我想获得带有 2 个 ID 的价格。该程序从 2 个不同的页面运行,这就是价格有 2 个 ID 的原因。我使用了 try 和 except 但它不起作用。我附上了代码。 现在我得到 IndexError: list index out of range。我将不胜感激。如果你想问我任何问题。

from selenium import webdriver
import csv

# set the proxies to hide actual IP

proxies = {
    'http': 'http://218.50.2.102:8080',
    'https': 'http://185.93.3.123:8080',
}

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % proxies)

driver = webdriver.Chrome(executable_path="C:\\Users\Andrei\Downloads\chromedriver_win32\chromedriver.exe",
                          chrome_options=chrome_options)
header = ['Product title', 'Product price']

with open('csv/products.csv', "w") as output:
    writer = csv.writer(output)
    writer.writerow(header)
links = ['https://www.amazon.com/Windsor-Glider-Ottoman-White-Cushion/dp/B017XRDV5S/ref=sr_1_1?s=home-garden&ie=UTF8&qid=1520265105&sr=1-1&keywords=-gggg&th=1',
         'https://www.amazon.com/Instant-Pot-Multi-Use-Programmable-Packaging/dp/B00FLYWNYQ/ref=sr_1_1?s=home-garden&ie=UTF8&qid=1520264922&sr=1-1&keywords=-gggh']
for i in range(len(links)):
    driver.get(links[i])
    product_title = driver.find_elements_by_xpath('//*[@id="productTitle"][1]')
    prod_title = [x.text for x in product_title]
    try:
        product_price = driver.find_elements_by_xpath('//*[@id="priceblock_ourprice"][1]')
        prod_price = [x.text for x in product_price]
    except:
        print('no price v1')
    try:
        product_price = driver.find_elements_by_xpath('//*[@id="_price"][1]')
        prod_price = [x.text for x in product_price]
    except:
        print('no price v2')
        
    csvfile = 'csv/products.csv'

    data = [prod_title[0], prod_price[0]]

    with open(csvfile, "a", newline="") as output:
        writer = csv.writer(output)
        writer.writerow(data)

【问题讨论】:

  • 请改进您的question
  • 只要运行代码,你就会明白我的意思
  • 考虑搜索您感兴趣的特定标签:prod_price = driver.find_element_by_xpath('//span[@id='priceblock_ourprice']').text
  • 我使用了 prod_price = driver.find_element_by_xpath('//span[@id="priceblock_ourprice"]').text 并且我得到了相同的 IndexError: list index out of range。可以请你运行程序看看吗?
  • 第二个链接如何使用 driver.find_element_by_xpath?

标签: python xpath webdriver


【解决方案1】:

好的。我想我发现了你的问题

您正在搜索带有find_elements_by_xpath 的元素列表。在这种情况下,当没有找到任何东西时,selenium 不会抛出异常。它返回一个空列表。所以prod_price = [x.text for x in product_price] 赋值在两个 try..except 子句中都有效。最后你可能有一个空的 prod_price。

您需要检查 prod_price 是否为空,然后才搜索替代 xpath

prod_price = [x.text for x in product_price]
if not prod_price:
    print('no price v')
    product_price = driver.find_elements_by_xpath(......

或使用引发异常的find_element_by_xpath,使用 xpath 进行一个元素搜索

try:
    product_price = driver.find_element_by_xpath('(//*[@id="priceblock_ourprice"])[1]')
    prod_price = product_price.text
except:
.........

P.S.您可以通过可迭代使用pythonic迭代方式

for link in links:
    driver.get(links)

【讨论】:

  • 不客气。我添加了 PS,关于循环列表。
猜你喜欢
  • 2014-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-23
  • 2022-06-14
  • 2015-02-11
  • 2018-02-08
  • 1970-01-01
相关资源
最近更新 更多