简体   繁体   中英

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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM