简体   繁体   English

如何使用Python Selenium选择输入元素

[英]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. 我只是找不到通过CSS和xpath尝试选择此元素的工作方法,但是没有任何效果。

<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() 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. 第一次调用可能无法工作,因为input后代节点很可能过于模糊和模棱两可。

The second invocation doesn't work because .submit[value='Submit'] is searching for (in english) 第二次调用无效,因为.submit[value='Submit']正在搜索(英文)

Any element that has class~="submit" AND value="Submit" 任何具有class~="submit" AND value="Submit"元素

The value attribute matches, but not the class selector. value属性匹配,但类选择器不匹配。

You could find that element with a quick CSS selector: 您可以使用快速的CSS选择器找到该元素:

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. 请参阅有效的CSS选择器,以了解如何制定良好的CSS选择器以及为什么该选择器可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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