【问题标题】:how to find xpath, class name of elements python selenium如何找到xpath,元素的类名python selenium
【发布时间】:2014-08-08 06:18:24
【问题描述】:

我的代码是:

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 是否正确。有人可以验证它们,请让我知道如何找到它们

【问题讨论】:

  • 没有你的 HTML 没什么好说的
  • 他在第一行给出了一个url。

标签: python selenium xpath classname


【解决方案1】:

在 Firefox 中,您可以简单地使用 developer toolsfirebug 来检查 html 中的类和元素 ID。在您问题中的link 之后,我可以找到一个名为 photo 的类,但对于 linkPrice 和 price,您应该使用其他类。

尝试:

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'

【讨论】:

  • 为什么我无法获取产品名称它取代了 noneprint productName[i]
  • 以上更改的结果:$496 none
  • 我在 elem 附近的一些地方弄错了,可能是 get_attribute("alt") 没有得到任何东西 for i in range(0, 16): elem=link[i].get_attribute("alt") productName.append(elem) print price[i].text print productName[i]
【解决方案2】:

您可以使用 chrome 中的开发者工具控制台验证 Xpath,例如 $x("//foo") 或 $(".foo") Firebug for Firefox 也可以让你验证

浏览器也会为您推荐 Xpath,但这些通常很冗长且不稳定,因此建议您手工制作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    相关资源
    最近更新 更多