Home > Mobile >  I'm having trouble selectiong an element using Selenium with Python
I'm having trouble selectiong an element using Selenium with Python

Time:10-01

I want to read out the text in this html element using selenium with python. I just can't find a way to find or select it without using the text (i don't want that because its content changes)

<div font-size="14px" color="text" class="sc-gtsrHT jFEWVt">0.101 ONE</div>

Do you have an idea how i could select it? The conventional ways listed in the documentation seem to not work for me. To be honest i'm not very good with html what doesn't make things any easier.

Thank you in advance

CodePudding user response:

Try this :

element = browser.find_element_by_class_name('sc-gtsrHT jFEWVt').text

Or use a loop if you have several elements :

elements = browser.find_elements_by_class_name('sc-gtsrHT jFEWVt')
for e in elements:
    print(e.text)

CodePudding user response:

print(browser.find_element_by_xpath("//*[@class='sc-gtsrHT jFEWVt']").text)

You could simply grab it by class name. It's 2 class names so it would be like so. by_class_name only uses one.

If the class name isn't dynamic otherwise you'd have to right click and copy the xpath or find a unique identiftier.

CodePudding user response:

You can do that using css selector.

driver.find_element_by_css_selector("div.sc-gtsrHT.jFEWVt").text

CodePudding user response:

Find by XPath as long as font size and color attribute are consistent. Be like,

//div[@font-size='14px' and @color='text' and starts-with(@class,'sc-')]

I guess the class name is random?

  • Related