简体   繁体   中英

Selenium Python XPATH how to select input field which is in post form?

I have two form in same page like below

<form method="get">
     <input type="submit">
</form>

<form method="post">
     <input type="submit">
</form>

I am able to get last input field like

//input[last()]

I need to track the input field if it's a post method form input.

Try following xpath:

.find_element_by_xpath('//form[@method="post"]//input')

Or the css selector looks better:

.find_element_by_css_selector('form[method="post"] > input')

You can get the list of forms using the XPath. Then extract its method attribute as below:

//form/[@method='post'] 

Once you have the form you can get the descendant as below:

//form/descendant::input[@type='submit']

This should return you the value.

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