请告诉我验证弹出窗口的设置用户名和密码的代码
System.setProperty("webdriver.chrome.driver","Driver//chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://admin3-dev.ca.primus");
WebDriverWait wait = new WebDriverWait(driver, 10);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword("nsingh","Primus01"));
上述代码中的身份验证弹出窗口中未传递用户名和密码。请告诉我如何点击登录按钮。
答案 0 :(得分:2)
您可以直接在URL中传递用户名和密码,如下所示: -
driver.get("http://UserName:Password@yoururl.com");
参考下面的链接: -
希望它会对你有所帮助:)。
答案 1 :(得分:1)
我认为您必须使用Robot才能添加此类
class SmartRobot extends Robot {
public SmartRobot() throws AWTException {
super();
}
public void pasteClipboard() {
keyPress(KeyEvent.VK_CONTROL);
keyPress(KeyEvent.VK_V);
delay(50);
keyRelease(KeyEvent.VK_V);
keyRelease(KeyEvent.VK_CONTROL);
}
public void type(String text) {
writeToClipboard(text);
pasteClipboard();
}
private void writeToClipboard(String s) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable transferable = new StringSelection(s);
clipboard.setContents(transferable, null);
}
}
并在您的代码中更改此
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword("nsingh","Primus01"));
由此
driver.switchTo().alert();
SmartRobot robot = new SmartRobot();
robot.type(USERNAME);
robot.keyPress(KeyEvent.VK_TAB);
robot.type(PASSWORD);
robot.keyPress(KeyEvent.VK_ENTER);
答案 2 :(得分:1)
针对不同的浏览器有不同的解决方案,但很少有程序化解决方案适用于所有浏览器。
alert.authenticateUsing
(在撰写本文时仅限IE)与身份验证对话框进行交互。如果不使用第三方GUI自动化框架,那么应该在所有浏览器中运行的东西就是设置一个可以添加基本身份验证标头或任意标头的代理(例如BrowserMob Proxy,{{3} },Fiddler)并将浏览器设置为通过它进行代理。
另请参阅mitmproxy以及与之相关的答案。
答案 3 :(得分:0)
适用于chrome
,仍在寻找firefox
浏览器。
System.setProperty("webdriver.chrome.driver","Driver//chromedriver.exe");
driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://admin3-dev.magma.ca");
Robot rb =new Robot();
rb.keyPress(KeyEvent.VK_P);
rb.keyRelease(KeyEvent.VK_P);
rb.keyPress(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_C);
rb.keyPress(KeyEvent.VK_H);
rb.keyRelease(KeyEvent.VK_H);
rb.keyPress(KeyEvent.VK_A);
rb.keyRelease(KeyEvent.VK_A);
rb.keyPress(KeyEvent.VK_L);
rb.keyRelease(KeyEvent.VK_L);
rb.keyPress(KeyEvent.VK_L);
rb.keyRelease(KeyEvent.VK_L);
rb.keyPress(KeyEvent.VK_A);
rb.keyRelease(KeyEvent.VK_A);
rb.keyPress(KeyEvent.VK_TAB);
rb.keyRelease(KeyEvent.VK_TAB);
rb.keyPress(KeyEvent.VK_CAPS_LOCK);
rb.keyRelease(KeyEvent.VK_CAPS_LOCK);
rb.keyPress(KeyEvent.VK_A);
rb.keyRelease(KeyEvent.VK_A);
rb.keyPress(KeyEvent.VK_CAPS_LOCK);
rb.keyRelease(KeyEvent.VK_CAPS_LOCK);
rb.keyPress(KeyEvent.VK_M);
rb.keyRelease(KeyEvent.VK_M);
rb.keyPress(KeyEvent.VK_M);
rb.keyRelease(KeyEvent.VK_M);
rb.keyPress(KeyEvent.VK_U);
rb.keyRelease(KeyEvent.VK_U);
rb.keyPress(KeyEvent.VK_L);
rb.keyRelease(KeyEvent.VK_L);
rb.keyPress(KeyEvent.VK_U);
rb.keyRelease(KeyEvent.VK_U);
rb.keyPress(KeyEvent.VK_1);
rb.keyRelease(KeyEvent.VK_1);
rb.keyPress(KeyEvent.VK_2);
rb.keyRelease(KeyEvent.VK_2);
rb.keyPress(KeyEvent.VK_TAB);
rb.keyRelease(KeyEvent.VK_TAB);
rb.keyPress(KeyEvent.VK_ENTER);`enter code here`
rb.keyRelease(KeyEvent.VK_ENTER);
答案 4 :(得分:0)
我的项目中遇到了Authentication Proxy弹出问题。所以我尝试了以下解决方案,它工作正常。 当我们在安装环境中运行来自Selenium Web驱动程序的脚本后,需要进行安装以处理身份验证代理。
首先,你需要知道以下细节,
private static WebDriver initFirefoxDriver(String appURL)
{
System.out.println("Launching Firefox browser..");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("network.proxy.type", 1);
firefoxProfile.setPreference("network.proxy.autoconfig_url", "http://example.com/abc.pac");
firefoxProfile.setPreference("network.proxy.http", " abc-proxy.com");
firefoxProfile.setPreference("network.proxy.http_port", 8080);
WebDriver driver = new FirefoxDriver(firefoxProfile);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.navigate().to(appURL);
//driver.get(appURL);
return driver;
}