Selenium 如何处理;“需要认证”;硒弹
是否可以使用警报处理selenium中的“需要身份验证”弹出窗口,其中包含“用户名”和“密码”字段Selenium 如何处理;“需要认证”;硒弹,selenium,authentication,selenium-webdriver,Selenium,Authentication,Selenium Webdriver,是否可以使用警报处理selenium中的“需要身份验证”弹出窗口,其中包含“用户名”和“密码”字段 您可以检查警报弹出窗口。 为此,您需要导入以下内容: import org.openqa.selenium.security.UserAndPassword; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; 首先,您需要等到
您可以检查警报弹出窗口。 为此,您需要导入以下内容:
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());
然后可以使用selenium web驱动程序的身份验证方法
alertPopUp.authenticateUsing(new UserAndPassword("your_username", "your_password"));
还有另外一种快速检查的方法,
如果您只想验证警报的存在
try {
Alert alert = driver.switchTo().alert();
alert.accept();
} catch (NoAlertPresentException e) {
// Alert not available
e.printStackTrace();
}
我使用以下代码在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()
最近,我在测试基本身份验证弹出窗口时遇到了这个问题。解决办法是
driver.get('http://admin:admin@internet.herokuapp.com/basic_auth')
在URL本身中添加用户名和密码。我在chrome和firefox中试用过,两者都可以使用。不用AutoIt进行身份验证弹出窗口,您只需执行此操作即可
driver.get(http://Username:Password@SiteURL)
如果身份验证弹出窗口来自HTTP Basic Auth,则可以在URL中发送用户名和密码:
http://username:password@example.com/
;另请参阅“确定但使用哪种编程语言?”@BillalBEGUERADJ我想使用java编程语言处理selenium中弹出的“需要身份验证”对话框。查看此帖子,方法“AuthenticateSing”不存在!!自Selenium 3之后,“AuthenticateSing”已被删除。我用萨法里没有运气,这是去润滑的。别用这个,它对我有用。不管怎样,你建议用什么来代替这个?