简体   繁体   English

Selenium AttributeError:list对象没有属性find_element_by_xpath

[英]Selenium AttributeError: list object has no attribute find_element_by_xpath

I am attempting to perform some scraping of nutritional data from a website, and everything seems to be going swimmingly so far, until I run into pages that are formatted slightly different. 我试图从一个网站上进行一些营养数据的搜索,到目前为止,一切似乎都在游动,直到我遇到格式略有不同的页面。

Using selenium and a line like this, returns an empty list: 使用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')

print would return this: print会返回这个:

[]
[]
[]
[]
[]

But if I define out the element position, then it works fine: 但是,如果我定义元素位置,那么它工作正常:

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

The issue I have ran into, is when the elements are not the same from page to page as I iterate. 我遇到的问题是,当迭代时,页面之间的元素不同。 So if a div does not exist in position 9, then an error is thrown. 因此,如果位置9中不存在div,则会引发错误。

Now when I go back and try to edit my code to do a try/catch , I am getting: 现在当我回去尝试编辑我的代码来try/catch ,我得到:

AttributeError: 'list' object has no attribute 'find_element_by_xpath' AttributeError:'list'对象没有属性'find_element_by_xpath'

or 要么

AttributeError: 'list' object has no attribute 'find_elements_by_xpath' AttributeError:'list'对象没有属性'find_elements_by_xpath'

Here is the code, with my commented out areas from my testing back and forth. 这是代码,我来回测试的注释区域。

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()

I had it working earlier to produce a list, and output to a CSV, but on occasion, the position count would come out wrong. 我让它早先工作生成一个列表,并输出到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']

The problems started when position 9 didn't exist on a particular page. 当特定页面上不存在位置9时,问题就开始了。

Are there any suggestions on how to fix this headache? 关于如何解决这个头痛有什么建议吗? Do I need to have cases set up for the different pages & sizes? 我是否需要为不同的页面和大小设置案例?

I appreciate the help. 我很感激帮助。

Actually find_elements() returns either list of WebElement or empty list. 实际上, find_elements()返回WebElement列表或空列表。 You're storing this result into a list variable name data . 您将此结果存储到列表变量名称data

AttributeError: 'list' object has no attribute 'find_element_by_xpath' AttributeError:'list'对象没有属性'find_element_by_xpath'

AttributeError: 'list' object has no attribute 'find_elements_by_xpath' AttributeError:'list'对象没有属性'find_elements_by_xpath'

This occurs because you're going to find nested WebElement on data list that's why you're calling as data.find_element_by_xpath() or data.find_elements_by_xpath() which is absolutely wrong. 之所以会发生这种情况,是因为您要在data列表中找到嵌套的WebElement ,这就是您调用data.find_element_by_xpath()data.find_elements_by_xpath()这绝对错误的原因。

Actually find_element() or find_elements() is used to search the element on the page context or the context of the WebElement instead of list . 实际上, find_element()find_elements()用于搜索页面上下文中的元素或WebElement上下文而不是list

So you should try to find individual WebElement from the data list and then find further nested WebElement using this element context as below :- 因此,您应该尝试从data列表中查找单个WebElement ,然后使用此元素上下文查找更多嵌套WebElement ,如下所示: -

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM