2016-09-06 2 views
2

find_element_by_xpath何の属性を持っていない、とすべてが、私は少し異なるフォーマットされているページに実行されるまで、これまでswimmingly行くことのようです。パイソン - セレンはAttributeError:リストオブジェクトは、私はウェブサイトからの栄養データのいくつかのスクレイピングを実行しようとしています

[] 
[] 
[] 
[] 
[] 

をしかし、私は要素の位置を定義した場合、それは正常に動作します:

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 

私が遭遇した問題は、要素がページごとに同じではない場合です私は繰り返す。 divの位置9に存在しないのであれば、エラーがスローされます。私は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が存在しない場合に問題が発生しました。

は、この頭痛を解決する方法上の任意の提案はありますか?私は別のページ&サイズの設定例を持っている必要がありますか?

私は助けていただきありがとうございます。

答えて

1

実際find_elements()リターンWebElementのリストまたは空のリストのいずれか。あなたは、リスト変数名dataにこの結果を保存しています。

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

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

あなたは絶対に間違っているdata.find_element_by_xpath()として、あるいはdata.find_elements_by_xpath()あなたが呼んでいる理由だネストされたWebElementdataのリストを見つけるつもりだので、これが発生しました。

実際find_element()又はfind_elements()ページコンテキスト又はWebElement代わりにlist文脈上の要素を検索するために使用されます。

だから、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 

    ---------------------------- 
    ---------------------------- 
+1

恐ろしいです。ありがとうございました。 – PDGill

関連する問題