github登錄頁面上只有一個表單,因此識別在這裏不是真正的問題。如果你想知道如何選擇,而無需使用getElementByID
的元素,您可以使用querySelector("...")
代替:
示例代碼
WebClient webClient = new WebClient(BrowserVersion.CHROME);
String url = "https://github.com/login";
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
HtmlPage page = webClient.getPage(url);
DomElement form = (DomElement) page.querySelector("form");
System.out.println(form.asXml());
webClient.close();
輸出
<form accept-charset="UTF-8" action="/session" data-form-nonce="0cd9f59e177729dbfe5a1b275514fdcc21be8c84" method="post">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓"/>
<input name="authenticity_token" type="hidden" value="3rrjjZbyJ6n310XnDR9mXCi5pJ6OsA+HvLJ0oem8k/XHj37Sd26GXxG7IQk5tcbDnPQnE7WvIjNgU77428iajw=="/>
</div>
<div class="auth-form-header p-0">
<h1>
Sign in to GitHub
</h1>
</div>
<div id="js-flash-container">
</div>
<div class="auth-form-body mt-3">
<label for="login_field">
Username or email address
</label>
<input autocapitalize="off" autocorrect="off" autofocus="autofocus" class="form-control input-block" id="login_field" name="login" tabindex="1" type="text"/>
<label for="password">
Password
<a href="/password_reset" class="label-link">
Forgot password?
</a>
</label>
<input class="form-control form-control input-block" id="password" name="password" tabindex="2" type="password"/>
<input class="btn btn-primary btn-block" data-disable-with="Signing in…" name="commit" tabindex="3" type="submit" value="Sign in"/>
</div>
</form>
哇,非常感謝!它像魅力一樣工作,你拯救了我的一天! –
非常歡迎您,很高興我能提供幫助。 –