chitcharonko
chitcharonko

Reputation: 331

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?

Upvotes: 1

Views: 297

Answers (1)

Artjom B.
Artjom B.

Reputation: 61892

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

Upvotes: 2

Related Questions