0

i'm trying to find with selenium in python a button in a website and the button identify is: AB 12

I'm trying with this code but all can't find the button...

driver.find_element_by_xpath("//button[contains(@class, '.size-grid-dropdown .size-grid-button') and contains(.,'AB 12)]")

driver.find_element_by_xpath('//button[span[text()="AB 12"]')

driver.find_element_by_link_text("AB 12")

the problem is: i have a lot of button with same class but different AB 12 / AB 23 / AB 34...

How can i find a precisely button?

Thanks!

3
  • Could you please post the DOM screenshot. so that we can try to check for some other attributes too. Commented Jun 1, 2020 at 11:56
  • find all by class => you'll get a list of buttons => loop through => check if text is "AB 12" . there is no other unique identity for the button as per the DOM you've given Commented Jun 1, 2020 at 11:56
  • Does //button[text()="AB 12"] work? It's a modified version of the second one you tried, without the span. Commented Jun 1, 2020 at 11:59

1 Answer 1

0

Presuming the id of the button is AB 12, the code you want is:

driver.find_element_by_id('AB 12')

you can see this answer for a similar query: >How to use find_element_by_id() in python or read the docs for the same here: https://selenium-python.readthedocs.io/api.html#locate-elements-by

In hindsight i see you want to find a button element with the text "AB 12" the correct code for that would be:

driver.find_element_by_xpath('//button[text()="AB 12"]')

You can check this similar query for more variations: How to find button with Selenium by its text inside (Python)?

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I see you used the code inside the blockquote , that would be incorrect for your question , i kept it for context, try the code at the end of the answer, i.e driver.find_element_by_xpath('//button[text()="AB 12"]')
Yeah that work... Thanks so much, I've been trying for a long time... Correct Answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.