Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find submit button in selenium without id

Tags:

I have a button

<input type="submit" class="button button_main" style="margin-left: 1.5rem;" value="something">

I cannot find it by id or name and need to submit a form.

I tried doing this: Alternatively, WebDriver has the convenience method “submit” on every element. If you call this on an element within a form, WebDriver will walk up the DOM until it finds the enclosing form and then calls submit on that. If the element isn’t in a form, then the NoSuchElementException will be raised: element.submit() http://selenium-python.readthedocs.org/navigating.html

But that cannot find the submit selector either.

ideas?

like image 367
humanbeing Avatar asked Feb 21 '16 01:02

humanbeing


1 Answers

There are many options here, to name a few:

If class alone is unique, you can use

driver.find_element_by_css_selector(".button_main").click()

If class + value combo is unique, you can use:

driver.find_element_by_css_selector(".button_main[value='something']").click()

You can also use xpath:

driver.find_element_by_xpath("//input[@type='submit' and @value='something']").click()

If none of those work (i.e. they are not identifying button uniquely), look at the elements above the button (for example <form) and provide the xpath in format:

driver.find_element_by_xpath("//unique_parent//input[@type="submit" and @value='something']").click()
like image 60
ytrewq Avatar answered Sep 18 '22 13:09

ytrewq