"Selenium AttributeError:列表对象没有find_element_by_xpath属性"

2024-05-19 18:18:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从一个网站上抓取一些营养数据,到目前为止,一切似乎进展顺利,直到我遇到格式略有不同的页面。

使用selenium和这样的行返回空列表:

values = browser.find_elements_by_class_name('size-12-fl-oz' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value')

打印将返回:

[]
[]
[]
[]
[]

但是如果我定义了元素的位置,那么它就可以工作了:

kcal = data.find_elements_by_xpath("(.//div[@class='size-12-fl-oz nutrition-value' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value'])[position()=1]").text

我遇到的问题是,当我迭代时,每页的元素都不相同。因此,如果位置9中不存在div,则抛出错误。

现在,当我返回并尝试编辑代码以执行try/catch操作时,我得到:

AttributeError: 'list' object has no attribute 'find_element_by_xpath'

或者

AttributeError: 'list' object has no attribute 'find_elements_by_xpath'

这是代码,我在测试中来回地注释掉了一些区域。

import requests, bs4, urllib2, csv
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException    

browser = webdriver.Firefox()
...

#Loop on URLs to get Nutritional Information from each one.
with open('products.txt') as f:
    for line in f:
        url = line
#        url = 'http://www.tapintoyourbeer.com/index.cfm?id=3'
        browser.get(url)
        with open("output.csv", "a") as o:
            writeFile = csv.writer(o)
            browser.implicitly_wait(3)
            product_name = browser.find_element_by_tag_name('h1').text.title() #Get product name
            size = browser.find_element_by_xpath("(//div[@class='dotted-tab'])").text #Get product size
            data = browser.find_elements_by_xpath("//table[@class='beer-data-table']")
#            values=[]
#            values = browser.find_elements_by_class_name('size-12-fl-oz' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value')
            try:
#            values = data.find_elements_by_xpath("(.//div[@class='size-12-fl-oz nutrition-value' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value'])")
                kcal = data.find_element_by_xpath("(.//div[@class='size-12-fl-oz nutrition-value' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value'])[position()=1]").text
                kj = data.find_element_by_xpath("(.//div[@class='size-12-fl-oz nutrition-value' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value'])[position()=3]").text
                fat = data.find_element_by_xpath("(.//div[@class='size-12-fl-oz nutrition-value' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value'])[position()=5]").text
                carbs = data.find_element_by_xpath("(.//div[@class='size-12-fl-oz nutrition-value' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value'])[position()=7]").text
                protein = data.find_element_by_xpath("(.//div[@class='size-12-fl-oz nutrition-value' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value'])[position()=9]").text
                values = [kcal, kj, fat, carbs, protein]
                print values
                writeFile.writerow([product_name] + [size] + values)
            except NoSuchElementException:
                print("No Protein listed")
browser.quit()

我让它早些时候产生一个列表,并输出到CSV,但有时,位置计数会出现错误。

[u'Budweiser', u'12 FL OZ', u'145.00', u'', u'', u'', u'']
[u"Beck'S", u'12 FL OZ', u'146.00', u'610.86', u'0.00', u'10.40', u'1.80']
[u'Bud Light', u'12 FL OZ', u'110.00', u'460.24', u'0.00', u'6.60', u'0.90']
[u'Michelob Ultra', u'12 FL OZ', u'95.00', u'397.48', u'0.00', u'2.60', u'0.60']
[u'Stella Artois', u'100 ML', u'43.30', u'KCAL/100 ML', u'181.17', u'KJ/100 ML', u'0.00']

当位置9在特定页面上不存在时,问题就开始了。

有什么治头痛的建议吗?我需要为不同的页面和大小设置案例吗?

我很感激你的帮助。


Tags: ordivbrowserdatasizebyvaluefind
1条回答
网友
1楼 · 发布于 2024-05-19 18:18:42

实际上,find_elements()返回^{}列表或空列表。您正在将此结果存储到列表变量名data

AttributeError: 'list' object has no attribute 'find_element_by_xpath'

AttributeError: 'list' object has no attribute 'find_elements_by_xpath'

发生这种情况是因为您将在data列表中找到嵌套的^{},这就是您调用data.find_element_by_xpath()data.find_elements_by_xpath()的原因,这是绝对错误的。

实际上,^{}^{}用于搜索页面上下文或^{}上下文中的元素,而不是list

因此,您应该尝试从data列表中找到单独的^{},然后使用下面的元素上下文进一步找到嵌套的^{}

if len(data) > 0:
  #now find desire element using index
  individual_element = data[0]

  #now you can find further nested single element using find_element() or list of elements using find_elements() at individual_element context
  kcal = individual_element.find_element_by_xpath("(.//div[@class='size-12-fl-oz nutrition-value' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value'])[position()=1]").text

  ----------------------------
  ----------------------------

相关问题 更多 >