Bala Murugan Bala Murugan - 1 month ago 5
Python Question

Same button id in python selenium

I have face the problem on clicking button on Python Selenium with same id
I can Have two buttons in one page named 'Upload' and 'Generate Base Form' but the two buttons id is same.
I will select the Button named "Generate Base Form". The id of the Button is also same.
I will attach the image of inspector
Image of Id for button

Answer

Although it's a good practice to find elements by Id, Name or CssClass, in some cases you'll end up depending on more specific properties, so that you can exclusively locate the element. The most common solution is using XPath.

In your case, something like

driver.find_element_by_xpath("//button[@id='uploadButton' and @value='Upload']").click()

Should work. Notice that if the other button also has the same value attribute, you'll need to discover what's unique about the element you're trying to locate, and use it with XPath.

Alternatively, you could also do

driver.find_element_by_xpath("(//button[@id='uploadButton'])[buttonNumber]").click()

Where buttonNumber is an index that goes from 1 to n, accordingly to the number of buttons you have with that same id.

This article can help you with locating elements.