Selenium Python - Extract Text from Class

Foxy

I'm trying to extract the text from the second "deep" class in the following image. In this case it would be the word "Sauvage"

The Elements

I've done the following:

search_perfumes = driver.find_elements(By.XPATH,'//span[@class="deep"][1]')
for perfumes in search_perfumes:
    list_perfumes.append(perfumes.text)

The length of the list correctly shows 23 elements (which is correct since the page has 23 perfumes), but the list has 23 empty elements. I can't seem to extract the text following the "deep" class.

Any idea on where I might be going wrong?

cruisepandey

Based on the Html that you've shared, you can use XPath indexing:

(//span[@class='deep'])[2] 

in code:

print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "(//span[@class='deep'])[2]"))).text)

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Now you must ensure that [2] represent Sauvage in the entire HTML. You can increase or decrease indices from [2] to any other matching number.

How would you do that? - You need to make sure that we have a unique matching node in HTMLDOM. Please see below for more detailed explanation:

Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

Also, you can have a list of web elements with this xpath //span[@class='deep']

for ele in driver.find_elements(By.XPATH, "//span[@class='deep']"):
    print(ele.text)

Update:

You have to click on Accept all cookie button first which is in shadow root:

Code:

driver = webdriver.Chrome(driver_path)

driver.maximize_window()
wait = WebDriverWait(driver, 30)

driver.get("https://www.parfumdreams.pt/?m=5&search=sauvage")

try:
    time.sleep(2)
    cookie_btn = driver.execute_script('return document.querySelector("#usercentrics-root").shadowRoot.querySelector("#uc-center-container > div.sc-jJoQJp.dTzACB > div > div > div > button")')
    cookie_btn.click()
    print('Clicked')
except:
    print('Could not click')
    pass


print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "(//span[@class='deep'])[2]"))).text)

or in case you want all of them, instead of the above print command, use the below code:

for ele in driver.find_elements(By.XPATH, "//span[@class='deep']"):
    driver.execute_script("arguments[0].scrollIntoView(true);", ele)
    print(ele.text)

Output:

DIOR
Sauvage
DIOR
Sauvage
DIOR
Sauvage
DIOR
Sauvage
DIOR
Sauvage
DIOR
Sauvage
DIOR
Sauvage
DIOR
Eau Sauvage
DIOR
Eau Sauvage
DIOR
Eau Sauvage
DIOR
Eau Sauvage
DIOR
Eau Sauvage
DIOR
Eau Sauvage
DIOR
Eau Sauvage
DIOR
Eau Sauvage
DIOR
Eau Sauvage
DIOR
Eau Sauvage
DIOR
Eau Sauvage
Creed
Neroli Sauvage
DIOR
Eau Sauvage
DIOR
Lápis de lábios
DIOR
Lápis de lábios
Estée Lauder
Maquilhagem para lábios

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to extract text from div class using Selenium with Python

Extract text from webpage using Selenium in Python

Extract h1 text from div class with scrapy or selenium

Selenium code to wait until CSS class is available and extract text in Python

Extract text under div class using Selenium and Python

Python: extract class and text

Python Selenium text extract list output from for loop

How to extract text from svg using python selenium

How to extract the text from the HTML using Selenium and Python

How to extract the text $7.56 from the webpage using Selenium through Python

Python/Selenium - how to extract text from modal fade content?

Extract text from an aria-label selenium webdriver (python)

How to extract the text from the webelements using Selenium and Python

Python 3.10: I cannot extract text from an element with Selenium 4.8.0

How to extract all href from a class in Python Selenium?

Extract href by class Selenium Python

Python Beautifulsoup extract text from different span with same class

Python: BeautifulSoup extract all the heading text from div class

Extract Text with Python from Webpages within div class

Python Selenium: How to get text from span class

How to find text from div class Selenium Python

How to extract the text 5 from the text node within the parent div node using Selenium and Python

How to extract text from Selenium Object?

How to extract text from an unordered list with selenium

Selenium Python extract text between Span

Extract text on mouse hover in python selenium

Extract text of website using Selenium and Python

How to extract multiple text using selenium python

How to extract the text elements using Selenium in Python?