我有以下代码,用我的UIWebView
填充网站登录表单的用户名和密码字段,并带有一些凭据。
NSString *usernamescript = @"document.getElementById('h_username').value='testingusername';";
[self.scrape stringByEvaluatingJavaScriptFromString:usernamescript];
NSString *passwordscript = @"document.getElementById('h_password').value='testingpassword';";
[self.scrape stringByEvaluatingJavaScriptFromString:passwordscript];
这些成功地填充了输入字段,因为这两个元素都有一个我可以参考的个人id
。
但是,提交按钮没有可用于发送点击操作的元素,ID,名称,类或标记。
<span class="button"><input value="Log in" type="submit"/></span></div>
如何通过这些方法发送点击这个特定的提交按钮,以识别我试图引用的页面上的哪个按钮?
这是表单上下文中的按钮:
><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"/> <label class="placeholder" for="h_password" id="password-label">Password</label><input maxlength="150" name="h_password" id="h_password" type="password"/> <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"/>
也许有用的信息可能是:form id="homepageRegisteredLogin"
?
答案 0 :(得分:0)
document.querySelector("input[type=submit]").click();
&#13;
><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"/> <label class="placeholder" for="h_password" id="password-label">Password</label><input maxlength="150" name="h_password" id="h_password" type="password"/> <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"/>
&#13;
答案 1 :(得分:0)
这可能对您有所帮助。 。
NSString *performSubmitJS = @"var passFields = document.querySelectorAll(\"input[type='submit']\"); \
passFields[0].click()";
[webView stringByEvaluatingJavaScriptFromString:performSubmitJS];