從閱讀你的問題,我相信你正在解決你的問題不正確。由於我無法確定您在瀏覽器中處理的是哪種身份驗證,因此我想我可以向您展示一個表單和HTTP 401,它很可能涵蓋一般場景。如果您需要其他示例,請以不同的方式進行身份驗證,請告訴我們。
如果我誤解了您的請求,並且您確實想要掛接到手動啓動的瀏覽器實例(默認情況下未提供),您必須非常有創意。無論如何,這個問題的答案是here。
但是,據我的建議,我強烈建議您儘可能地去掉vb腳本/手動瀏覽器。如果您可以通過瀏覽器進行身份驗證,則可以通過硒進行身份驗證。下面是一些例子:
Note, this is a good guide on how to use the InternetExplorerDriver
方案1:使用HTML表單 的HTML代碼示例:
<form>
<table id="credentials_table">
<tbody>
<tr>
<td class="credentials_table_label_cell"><label for="username" id="label_input_1">Username</label></td>
<td class="credentials_table_field_cell"><input class="credentials_input_text" value="" id="username" autocomplete="off" autocapitalize="off" type="text"></td>
</tr>
<tr>
<td class="credentials_table_label_cell"><label for="password" id="label_input_2">Password</label></td>
<td class="credentials_table_field_cell"><input class="credentials_input_password" value="" id="password" autocomplete="off" autocapitalize="off" type="password"></td>
</tr>
<tr id="submit_row">
<td class="credentials_table_field_cell"><input class="credentials_input_submit" value="Logon" type="submit"></td>
</tr>
</tbody></table>
</form>
樣品Selenim代碼以登錄:
private WebDriver driverForA = new InternetExplorerDriver();
private WebDriver driverForB = new InternetExplorerDriver();
@After
public void after() {
driverForA.close();
driverForB.close();
}
@Test
public void testADoesThisAndBDoesThat() {
driverForA.get("http://my.login.url");
final WebElement usernameInput = driverForA.findElement(By.id("username"));
final WebElement passwordInput = driverForA.findElement(By.id("password"));
final WebElement submitButton = driverForA.findElement(By.xpath("//input[@type='submit' and @value='Logon']"));
// perform the login stuff
clearKeysAndSetValue(usernameInput, "Joe");
clearKeysAndSetValue(passwordInput, "superSecret");
submitButton.click();
// navigate to other pages and do things
driverForB.get("http://my.approval.page");
final WebElement approveButton = driverForB.findElement(By.id("approval_Button"));
approveButton.click();
}
private void clearKeysAndSetValue(final WebElement element, final String valueToSet) {
element.clear();
element.sendKeys(valueToSet);
}
Scenario 2: Using HTTP 401
private final WebDriver driverForA = new InternetExplorerDriver();
private final WebDriver driverForB = new InternetExplorerDriver();
@After
public void after() {
driverForA.close();
driverForB.close();
}
@Test
public void testADoesThisAndBDoesThatHttpBasic() {
// authenticate as a
WebDriverWait wait = new WebDriverWait(driverForA, 5);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword("Joe", "superSecret"));
// navigate to other pages and do things
driverForB.get("http://my.approval.page");
final WebElement approveButton = driverForB.findElement(By.id("approval_Button"));
approveButton.click();
}
爲什麼不打開一個新的webdriver而是創建一個vbs? – Madhan