Python,Chrome Xpath无法定位元素

2024-10-02 14:16:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个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">

我尝试编写此代码,但没有得到结果:

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)

对于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)

请告知,如何修复代码


Tags: 代码namedivbyh2elementfindbarcode
1条回答
网友
1楼 · 发布于 2024-10-02 14:16:02

通过css选择器获取它:

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

要获取值,请使用:

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

点是用来上课的

如果您更喜欢xpath

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

相关问题 更多 >

    热门问题