So you need to click a button within your selenium python test automation. You try to use “by xpath” (e.g. you copied the xpath value the element inspector in the browser) and it fails. You try to do the same with “by css” and “by class” and still the same result – it fails with an error.
What should you do?
The key is to find something that is unique for the element that you are trying to perform an action on (in your case a click) AND you are able to specify this uniqueness within your code / locator for the element.
Finding something unique can still be quite daunting and lead to a lot of trial and error until you get to something that works.
In these cases it’s useful to look for names that indicate that it’s unique, e.g. ‘id’ (short for identifier, which in a lot of cases are unique) – or even better something that was put in place to help with test automation, e.g. ‘automation-id’.
Example:
You have a button that has the following HTML:
<button automation-id="login-btn-sign-in" ... >Sign in</button>
So instead of
driver.find_element_by_xpath('/html/body/ui ... /div/div ... /button]').click()
try
driver.find_element_by_xpath('//button[@automation-id="login-btn-sign-in"]').click()