Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select input element with Python Selenium

I just cannot find working way to select this element, tried by CSS and xpath, but nothing works.

<input type="submit" value="Submit">

This does not work:

driver.find_element_by_xpath("//*[@id='theform']/div[2]/input").click()
driver.find_element_by_css_selector(".submit[value='Submit']").click()
like image 433
user3281831 Avatar asked Oct 25 '25 16:10

user3281831


1 Answers

This does not work:

driver.find_element_by_xpath("//*[@id='theform']/div[2]/input").click() driver.find_element_by_css_selector(".submit[value='Submit']").click()

The first invocation likely does not work because the input descendant node is most likely too vague and ambiguous.

The second invocation doesn't work because .submit[value='Submit'] is searching for (in english)

Any element that has class~="submit" AND value="Submit"

The value attribute matches, but not the class selector.

You could find that element with a quick CSS selector:

driver.find_element_by_css_selector("input[type='submit']")

See Effective CSS Selectors to see how to formulate good CSS selectors, and why this selector above would work.

like image 167
ddavison Avatar answered Oct 28 '25 05:10

ddavison



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!