Selenium AttributeError: list object has no attribute find_element_by_xpath
我正试图从一个网站上抓取一些营养数据,到目前为止,一切似乎都进展顺利,直到我遇到格式稍有不同的页面。
使用Selenium和这样的一行,返回一个空列表:
1 | 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') |
打印将返回此:
1 2 3 4 5 | [] [] [] [] [] |
号
但是如果我定义了元素的位置,那么它就可以正常工作了:
1 | 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,那么会抛出一个错误。
现在,当我返回并尝试编辑代码以执行
AttributeError: 'list' object has no attribute 'find_element_by_xpath'
号
或
AttributeError: 'list' object has no attribute 'find_elements_by_xpath'
号
这是代码,我在测试中反复注释掉了一些区域。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 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,但有时,位置计数会出错。
1 2 3 4 5 | [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在特定页面上不存在时,问题就开始了。
关于如何治疗头痛有什么建议吗?是否需要为不同的页面和大小设置案例?
我感谢你的帮助。
实际上,
AttributeError: 'list' object has no attribute 'find_element_by_xpath'
AttributeError: 'list' object has no attribute 'find_elements_by_xpath'
号
这是因为你会在
实际上,
因此,您应该尝试从
1 2 3 4 5 6 7 8 9 | 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 ---------------------------- ---------------------------- |