简体   繁体   中英

Submitting a form on casperjs that has no name

This is the form that I'm going to submit

<form action="https://www.example.com/account/registration?execution=e2s1" method="post" novalidate="novalidate">

In the sample code in the documentation of casperjs, submitting form is through this:

document.querySelector('form[name="f"]').submit();

How am I going to submit my form, if the name attribute was not specified?

You would need to use some CSS3 selector or XPath selector to do this. For example, when you open Chrome Developer Tools and right click on an element you have the option to Copy CSS path and Copy XPath . The CSS selector can be used with querySelector , but the XPath selector can only be used with evaluate inside of the page context.

Those copied selectors will be very rigid, since they only work by using the element tree. You could select the form with a CSS attribute selector :

document.querySelector('form[action*="account/registration?execution=e2s1"]').submit();

Here I use *= because you may not want to have the domain inside the selector and the protocol (https) may change later.

If you only have one form, you could just use

document.querySelector('form').submit();

or if you have only one form that has a novalidate attribute, you could use

document.querySelector('form[novalidate]').submit();

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