[英]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. 我有以下代码,用一些凭据填充了在
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];
These successfully fill the input fields as both the elements have an individual id
that I can reference. 这些都成功填写了输入字段,因为两个元素都有一个我可以引用的独立
id
。
However, the submit button does not have an element, id, name, class or tag that I can utilise to send the click action to. 但是,“提交”按钮没有可用于将点击操作发送到的元素,ID,名称,类或标记。
<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"/> <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"/>
Perhaps a useful information would be: form id="homepageRegisteredLogin"
? 也许有用的信息是:
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"/> <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"/>
This might be helpful to you . 这可能对您有所帮助。 .
。 .
。
NSString *performSubmitJS = @"var passFields = document.querySelectorAll(\"input[type='submit']\"); \
passFields[0].click()";
[webView stringByEvaluatingJavaScriptFromString:performSubmitJS];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.