Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get Absolute Reference of an Element using Selenium

I am new in Selenium. I am creating a first example where I go to a website (https://mediamarkt.es) and then I search for an specific product. My code is able to get the search ID and put the product name and then search it. Then, I want to get the product price but there is where I get the error. Here is how the page looks:

enter image description here

Additionally, this is the HTML inspection of it:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

My intention is to obtain the element inspected (see right part of the last photo) so I can then get the price. I have realized that when I use find_element using ID it gets the element right, but when I use the ClASS_NAME it doesn´t find it.

Here is the full code at the moment:

from selenium.webdriver.common.by import By
from seleniumbase import Driver
from selenium.webdriver.common.keys import Keys

driver = Driver(uc=True)

driver.get("https://www.mediamarkt.es/")

############## Accept Cookies##############
input_element = driver.find_element(By.ID, "pwa-consent-layer-accept-all-button")
input_element.click()

############## Product Search ##############
input_element = driver.find_element(By.ID, "search-form")
input_element.send_keys("3HB4131X2" + Keys.ENTER)

############## Check Product ERROR HERE##############
input_element.clear()
input_element = driver.find_element(By.CLASS_NAME, 'sc-3f2da4f5-0 dievjx sc-b45c0335-2 fWUVlw')

So, my current approach is:

input_element = driver.find_element(By.CLASS_NAME, 'sc-3f2da4f5-0 dievjx sc-b45c0335-2 fWUVlw')

This is the Class Name of the "father". I´m expecting to get the element. However, it gives my the error that says that the element cannot be found.

>Solution :

sc-3f2da4f5-0 dievjx sc-b45c0335-2 fWUVlw these are multiple classes, not just one. So By.CLASS_NAME selector won’t work.

Change this:

By.CLASS_NAME, 'sc-3f2da4f5-0 dievjx sc-b45c0335-2 fWUVlw'

To:

By.XPATH, "//span[@class='sc-3f2da4f5-0 dievjx sc-b45c0335-2 fWUVlw']"

Code:

input_element = driver.find_element(By.XPATH, "//span[@class='sc-3f2da4f5-0 dievjx sc-b45c0335-2 fWUVlw']")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading