1

im trying to find an element with dinamic values , for example <span class="ms-Button-label label-175" id="id__177">Save</span> in inspect element, the id and class values tend to change for every refresh, how can i in this case find the element in selenium? i tried troguht xpath but seems doesnt work because can not find the path, i was thinking to find "Save" world torught always find by xpath but actually i dont know if im doing well : driver.find_element_by_xpath(//span(@.... but then? how can insert element if it changes everytime? thanks!

1 Answer 1

2

Something like this may work:

driver.find_element_by_xpath('//span[text()="Save"]')

But this will fail, if there is more than one button with text "Save" on the page.

In that case you may try to find some specific outer element (div, form, etc.) which does not change and contains the button. Then find the button inside of it.

With few requests with driver:

specific_div = driver.find_element_by_id("my_specific_div")
button = specific_div.find_element_by_tag_name("span")  # e.g. there is only one span in that div

Or with more specific xpath:

button = driver.find_element_by_xpath('//div[@class="some-specific-class"]/span[text()="Save"]')

If needed, search for more nested elements before the button, so you can get more narrow search field.

More examples in the docs.

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

Comments

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.