繁体   English   中英

Python Selenium - 如何根据 span 标签内的文本提取元素?

[英]Python Selenium - How to extract element based on text inside span tag?

我正在从 URLhttps://blinkit.com/prn/catch-cumin-seedsjeera-whole/prid/56692中提取一些具有非结构化产品详细信息元素的数据。

使用此代码:

 product_details = wd.find_elements(by=By.XPATH, value="//div[@class='ProductAttribute__ProductAttributesDescription-sc-dyoysr-2 lnLDYa']")
 info_shelf_life = product_details[0].text.strip()
 info_country_of_origin = product_details[1].text.strip()

如您所见,产品详细信息元素是非结构化的,当索引从 URL 更改为 URL 时,这种方法不适合

因此尝试了这种方法,它抛出了 NoSuchWindowException 错误。

info_shelf_life = wd.find_element(By.XPATH,value= "//div[[contains(@class, 'ProductAttribute__ProductAttributesDescription-sc-dyoysr-2 lnLDYa') and contains(., 'Shelf Life')]/..")
print(info_shelf_life.text.strip())

如何根据 span 标签内的文本提取 div 内的文本?

您的 XPath 无效。 你可以试试

info_shelf_life = wd.find_element(By.XPATH, '//p[span="Shelf Life"]/following-sibling::div').text
info_country_of_origin = wd.find_element(By.XPATH, '//p[span="Country of Origin"]/following-sibling::div').text

获取所需数据

暂无
暂无

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

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