2014-06-18 27 views
1

我的代碼是:如何找到的XPath,元素蟒蛇硒的類名

driver.get("http://www.thegoodguys.com.au/buyonline/SearchDisplay?pageSize=16&beginIndex=0&searchSource=Q&sType=SimpleSearch&resultCatEntryType=2&showResultsPage=true&pageView=image&searchTerm=laptops") 
link=(); 
linkPrice=(); 
price=(); 
productName=[]; 
Site='Harvey Norman' 
link=driver.find_elements_by_class_name("photo") 

linkPrice=driver.find_elements_by_class_name("product-title") 

price=driver.find_elements_by_xpath("//div[@class='purchase']/span/span") 

我不知道所提供的XPath和CLASS_NAME是否正確。有人可以驗證它們,請讓我知道如何找到它們

+0

這沒什麼可說的沒有你的HTML – olyv

+0

他給人的第一行的URL。 – Sebastian

回答

1

在Firefox中,你可以簡單地使用developer toolsfirebug檢查HTML類和元素IDS。在你的問題link之後,我可以找到一個叫做photo的類,但是對於linkPrice和價格你應該使用其他的類。

嘗試:

price=driver.find_elements_by_class_name("price") 
linkPrice=driver.find_elements_by_class_name("addtocart") 

這給了我:

price[0].text 
u'$496' 
linkPrice[0].text 
u'ADD TO CART' 
+0

爲什麼我不能取得產品名稱取代沒有'print productName [i]' – Jeevan

+0

上述變化的結果:'$ 496 none' – Jeevan

+0

我錯了一些接近elem的地方,可能是get_attribute(「alt」)取什麼都沒有'對於i在範圍(0,16): \t ELEM =連桿[I] .get_attribute( 「ALT」) \t productName.append(ELEM) \t打印價格[I]的.text \t打印PRODUCTNAME [I]' – Jeevan

1

您可以使用開發人員工具控制檯在Chrome中驗證Xpath,例如$ x(「foo」)或$(「。foo」) Firebug for Firefox還可以讓您驗證

而且瀏覽器會提示Xpath的你,但這些往往是冗長和不穩定所以建議手各具特色