2017-04-11 34 views
1

是否有可能使用警報處理具有「用戶名」和「密碼」字段的硒的「需要驗證」彈出窗口。如何處理「需要驗證」在硒中彈出

enter image description here

+4

如果驗證彈出窗口來自HTTP Basic Auth,您可以在URL中輸入用戶名和密碼:'http:// username:password @ example.com /';另請參閱http://serverfault.com/questions/371907/can-you-pass-user-pass-for-http-basic-authentication-in-url-parameters/371918 – rlandster

+0

可能的重複[如何處理認證彈出使用Java Selenium WebDriver](http://stackoverflow.com/questions/24304752/how-to-handle-authentication-popup-with-selenium-webdriver-using-java) – JeffC

+0

當然,但在哪種編程語言? –

回答

0

您可以檢查警報彈出。 爲此,你需要導入下面,

import org.openqa.selenium.security.UserAndPassword; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 

首先,你需要等待,直到彈出來了。

WebDriverWait wait = new WebDriverWait(driver, 30); 

然後檢查警報彈出存在/可見或不可見

Alert alertPopUp = wait.until(ExpectedConditions.alertIsPresent()); 

然後你可以使用硒網絡驅動器的authenticateUsing方法。

alertPopUp.authenticateUsing(new UserAndPassword("your_username", "your_password")); 

還有anoother的方式來快速檢查, 如果你只想驗證警報

try { 
    Alert alert = driver.switchTo().alert(); 
    alert.accept(); 

} catch (NoAlertPresentException e) { 
    // Alert not available 
    e.printStackTrace(); 
} 
0

現在我得到這個使用下面的代碼在IE工作:

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
import time 

driver = webdriver.Ie() 
driver.get("https://scdm.corp.vha.com") 
alert = driver.switch_to_alert() 
alert.authenticate("username","password") 
time.sleep(20) 
driver.close() 
相關問題