简体   繁体   English

Python,Chrome - Xpath 无法定位元素

[英]Python, Chrome - Xpath is Unable to Locate Element

I have this HTML and try to parse Barcode我有这个 HTML 并尝试解析条形码

</div>
<div class="bc">
<div class="pititle">
   <h2 itemprop="name">Bath Shelf - 5 Pack</h2>
</div>
<div class="brandmanu model">
   <h5>Product code</h5>
   <h6>BA0576</h6>
</div>
<div class="brandmanu gtin">
   <h5>Barcode</h5>
   <h6>5056170307192</h6>
</div>
<div class="brandmanu inner">
   <h5>Inner Barcode</h5>
   <h6>NO INNER</h6>
</div>
<div class="brandmanu outer">
   <h5>Outer barcode</h5>
   <h6>25056170307196</h6>
</div>
<div class="brandmanu brand" itemprop="brand" itemscope="" itemtype="http://schema.org/Brand">

I try to write this code, but don't get the result:我尝试编写这段代码,但没有得到结果:

element = driver.find_element_by_class_name("brandmanu gtin")
tag_list = driver.find_elements_by_xpath("//div[@class='brandmanu gtin']")
print(element)
print(tag_list)

For find_element_by_class_name I get the error:对于find_element_by_class_name我收到错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".brandmanu gtin"}
  (Session info: chrome=89.0.4389.90)

Please, advise, how to fix code.请告知如何修复代码。

Get it by css selector:通过css选择器获取:

element = driver.find_element_by_css_selector(".brandmanu.gtin>h5").text

To get the value use:要获得价值使用:

element = driver.find_element_by_css_selector(".brandmanu.gtin>h6").text

Dot is used for classes.点用于类。

If you prefer xpath :如果您更喜欢xpath

element = driver.find_element_by_xpath('//div[@class="brandmanu gtin"]/h5').text

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

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