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

yash agarwal

I am extracting some data from URLhttps://blinkit.com/prn/catch-cumin-seedsjeera-whole/prid/56692 with unstructured Product Details elements.

Using this code:

 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()

As you can see the Product details elements are unstructured and this approach is not suitable when the Index gets changed from URL to URL

Hence tried this approach, which throws out a NoSuchWindowException error.

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())

How can I extract text inside div based on text inside span tags?

JaSON

Your XPath is invalid. You can try

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

to get required data

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python Selenium: Click on button based on text inside span tag

How to find all the span tag inside of an element in selenium python?

How to get span tag text with selenium for python

Python Selenium Click button inside span tag using button text

How to get text which is inside the span tag using selenium webdriver?

How to extract the text inside a tag with BeautifulSoup in Python?

Selenium did not print text inside the span tag

How to extract only the text which is not inside any tag using xpath with selenium and python binding

How to extract text inside span without class name with BeautifulSoup in python

How can I get a text which in span tag like <span>Test</span> by using python selenium?

Unable to get the text data, where dynamic number of span tag are inside p tag- Selenium Python

Selenium Python extract text between Span

Get text from span tag with Python and Selenium

How to get text inside one span tag to another span tag?

Get text inside a <p> element inside span tag with VBA

Unable to Extract text names in span tag using selenium webdriver

How to extract a span tag inside div another tag

Python & Selenium: Find element by text in span class

How to read text in Span tag when multiple span in Selenium

extract information inside span tag

How to find a element with specific text in <span> with Selenium

How would I show text inside a span tag when hovering over a different element?

How do i select the text of this li element but not the text of the span element within using selenium python?

How to extract the text within the element using Selenium WebDriver and Python?

How to locate an element and extract required text with Selenium and Python

unique xpath for element span inside div inside button selenium python

How do I obtain the text inside a label which is inside a span using Selenium with Python?

How to wrap the specific text with span tag inside the <p> tag

how to extract text inside a tag with its tags?