简体   繁体   中英

Programmatically clicking 'submit' button in UIWebView (no element id, name, class or tag)

I have the following code that fills the username and password fields of a website login form loaded in my UIWebView with some credentials.

   NSString *usernamescript = @"document.getElementById('h_username').value='testingusername';";
    [self.scrape stringByEvaluatingJavaScriptFromString:usernamescript];
    NSString *passwordscript = @"document.getElementById('h_password').value='testingpassword';";
    [self.scrape stringByEvaluatingJavaScriptFromString:passwordscript];

These successfully fill the input fields as both the elements have an individual id that I can reference.

However, the submit button does not have an element, id, name, class or tag that I can utilise to send the click action to.

<span class="button"><input value="Log in" type="submit"/></span></div>

How can I send a click to this particular submit button with these means of identifying which button on the page I am trying to refer to?

This is the button in context of the form:

><div id="login"><div class="inner"><form id="homepageRegisteredLogin" action="/login/registeredUserUsernameAndPasswordLogin" method="post"><fieldset><legend class="off-screen">Login</legend><span class="label">Log in to my Opal account:</span><p id="loginErrorMessageBox" style="color: #fef7f5; background: #d21f01; text-align:center; margin: 0;display:none;"></p><div><label class="placeholder" for="h_username" id="username-label">Username</label><input maxlength="250" name="h_username" id="h_username" type="text"/>&nbsp;<label class="placeholder" for="h_password" id="password-label">Password</label><input maxlength="150" name="h_password" id="h_password" type="password"/>&nbsp;<input value="" name="attempt" type="hidden"/><span class="button"><input value="Log in" type="submit"/></span></div><a title="Forgot your username or password?" href="/login/forgotten">Forgot your username or password?</a></fieldset><div><input type="hidden" name="CSRFToken" value="28846d41-89b0-40ba-ae0f-6b644480385c"/>

Perhaps a useful information would be: form id="homepageRegisteredLogin" ?

在下面的评论后编辑

 document.querySelector("input[type=submit]").click(); 
 ><div id="login"><div class="inner"><form id="homepageRegisteredLogin" action="/login/registeredUserUsernameAndPasswordLogin" method="post"><fieldset><legend class="off-screen">Login</legend><span class="label">Log in to my Opal account:</span><p id="loginErrorMessageBox" style="color: #fef7f5; background: #d21f01; text-align:center; margin: 0;display:none;"></p><div><label class="placeholder" for="h_username" id="username-label">Username</label><input maxlength="250" name="h_username" id="h_username" type="text"/>&nbsp;<label class="placeholder" for="h_password" id="password-label">Password</label><input maxlength="150" name="h_password" id="h_password" type="password"/>&nbsp;<input value="" name="attempt" type="hidden"/><span class="button"><input value="Log in" type="submit"/></span></div><a title="Forgot your username or password?" href="/login/forgotten">Forgot your username or password?</a></fieldset><div><input type="hidden" name="CSRFToken" value="28846d41-89b0-40ba-ae0f-6b644480385c"/> 

This might be helpful to you . . .

 NSString *performSubmitJS = @"var passFields = document.querySelectorAll(\"input[type='submit']\"); \
        passFields[0].click()";
 [webView stringByEvaluatingJavaScriptFromString:performSubmitJS];

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