1

Probably the title is not 100% correct. I'm writing an app in Swift and after that I filled two field of a form I want to submit them. So the button is created in this way:

<button class="mb3 w-100 input-reset sans-serif pointer bg-blue hover-bg-white hover-blue bg-animate ba ph3 pv2 lh-copy white b--blue b br0" data-test="login-btn">Log in</button>

For fill the 2 field of the form I used:

webView.evaluateJavaScript("document.getElementsByName('email')[0].value='\(firstTf.text!)'", completionHandler: nil)

But I'm totally lost for what about submit the request... Any ideas? Thank you!

EDIT:

This is the form HTML code:

<form action="/login" class="measure center" data-test="login-form" method="POST"><input id="__anti-forgery-token" name="__anti-forgery-token" type="hidden" value="I/ulxtAepoMOjZvudrMhXDSqp89wIKM6hPkw7G6xHyYOqfcQIi3S/EVey1L7/CvHZI3SCbJ3Qa5rcl4j"><div class="mb3"><label class="db f6 mb2 b">Email</label><input class="mb1 pv2 lh-copy ph3 input-reset sans-serif black-80 ba bg-transparent w-100 b--black-20" name="email" type="email"><div class="f6 black-50 dn">Don't worry, we hate spam as much as you do</div></div><div class="mb3"><label class="db f6 mb2 b">Password</label><input class="pv2 lh-copy ph3 input-reset sans-serif ba bg-transparent w-100 b--black-20" name="password" type="password"></div><div class="mv3 lh-title"></div><div><button class="mb3 w-100 input-reset sans-serif pointer bg-blue hover-bg-white hover-blue bg-animate ba ph3 pv2 lh-copy white b--blue b br0" data-test="login-btn">Log in</button></div><div class="f6 black-50 tc lh-copy"><div><a class="link blue hover-dark-blue ml1 dib" data-test="forgot-password" href="/reset-password">Forgot your password?</a></div></div></form>

2 Answers 2

2

Fetch the element and then call .submit() on it.

webView.evaluateJavaScript("document.getElementById('FORM').submit()", completionHandler: nil)

something like this might do the job for you.

Since the form don't have an id, I assume that you don't have control over the website that you are showing, so adding an id is not an option. Because i don't have the whole page I would like to have an id to get from. You can navigate the DOM and get a child of the form that has an id and then ask for the parent.

document.getElementById('__anti-forgery-token').parentNode.submit()

This should invoke a submit of the form.

Sign up to request clarification or add additional context in comments.

2 Comments

I will try and I’ll let you know. Thanks for the answer !
Sorry, cause of the Time Zone difference I didn't have the time to test... Unfortunately it's not working because there is no ID "FORM" in any HTML lines
-1

I found the answer.Instead of use "submit()" on TagName, I used "click()" as follow:

webView.evaluateJavaScript("document.getElementsByTagName('button')[0].click()", completionHandler: nil)

7 Comments

That is not true, you can use submit() on getElementsByTagName its just a DOM selector, it does not determine the methods you can use on it.
Perhaps your code is wrong. It could be a subject for a new question. But what you are writing is not true.
Not at all... As you can see is the same as the previous, with the only difference that is not submit() but click()...
But button, don't have a submit() method a form has. Perhaps thats your problem.
Read my updated answer, getElementById is just a method to get a node from the DOM. getElementsByTagName is also just a selector for the DOM, it does not influence the methods a node has. I use getElementsById because the ID is unique and i don't know your whole DOM on the page you are looking, and I'm sure the parent is form.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.