Selenium 如何通过在单击链接从HTTP重定向到HTTPS时出现的询问基本身份验证凭据的窗口?
我有一个网站,大多数页面通常通过HTTP使用,但其他一些页面只能通过HTTPS使用。站点受基本身份验证保护(HTTP和HTTPS页面的凭据相同) 当我在浏览器中打开任何HTTP页面(FF或Chrome)并单击指向HTTPS页面的链接时,浏览器会显示请求基本身份验证凭据的警报 我对Webdriver(FF或Chrome)也有同样的问题:Selenium 如何通过在单击链接从HTTP重定向到HTTPS时出现的询问基本身份验证凭据的窗口?,selenium,webdriver,selenium-webdriver,basic-authentication,Selenium,Webdriver,Selenium Webdriver,Basic Authentication,我有一个网站,大多数页面通常通过HTTP使用,但其他一些页面只能通过HTTPS使用。站点受基本身份验证保护(HTTP和HTTPS页面的凭据相同) 当我在浏览器中打开任何HTTP页面(FF或Chrome)并单击指向HTTPS页面的链接时,浏览器会显示请求基本身份验证凭据的警报 我对Webdriver(FF或Chrome)也有同样的问题: 当我访问http://username:password@一些_domain.com,然后单击指向HTTPS页面的链接,将出现要求提供基本身份验证凭据的浏览器警报
当我访问
http://username:password@一些_domain.com
,然后单击指向HTTPS页面的链接,将出现要求提供基本身份验证凭据的浏览器警报窗口。Selenium不“记住”为HTTP页面输入的凭据
如何使用Webdriver执行此操作序列?如果不可能,你有什么建议
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("network.http.phishy-userpass-length", 255);
profile.SetPreference("network.automatic-ntlm-auth.trusted-uris", hostname);
Driver = new FirefoxDriver(profile);
主机名是您的URL(example.com),然后尝试
Driver.Navigate().GoToUrl(http://user:password@example.com);
到目前为止,我能想到的最好的解决方案是创建一个处理超时的新线程。由于WebDriver在FF和其他特定浏览器上不返回控制,我可以调用线程处理程序,然后使用Robot输入凭据并按enter键(这里也可以使用AutoIt)。然后该控件返回到WebDriver以继续使用脚本
//put this where it belongs, say calling a new url, or clicking a link
//assuming necessary imports
int pageLoadTimeout = 10;
String basicAuthUser = "user";
String basicAuthPass = "pass";
String url = "https://yourdomain.com";
WebDriver driver = new FirefoxDriver();
TimeoutThread timeoutThread = new TimeoutThread(pageLoadTimeout);
timeoutThread.start();
driver.get(url);
//if we return from driver.get() call and timeout actually occured, wait for hanlder to complete
if (timeoutThread.timeoutOccurred){
while (!timeoutThread.completed)
Thread.sleep(200);
}
else {
//else cancel the timeout thread
timeoutThread.interrupt();
}
public class TimeoutThread extends Thread {
int timeout;
boolean timeoutOccurred;
boolean completed;
public TimeoutThread(int seconds) {
this.timeout = seconds;
this.completed = false;
this.timeoutOccurred = false;
}
public void run() {
try {
Thread.sleep(timeout * 1000);
this.timeoutOccurred = true;
this.handleTimeout();
this.completed = true;
}
catch (InterruptedException e) {
return;
}
catch (Exception e){
System.out.println("Exception on TimeoutThread.run(): "+e.getMessage());
}
}
public void handleTimeout(){
System.out.println("Typing in user/pass for basic auth prompt");
try {
Robot robot = new Robot();
//type is defined elsewhere - not illustrating for this example
type(basicAuthUser);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
Thread.sleep(500);
type(basicAuthPass);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
catch (AWTException e) {
System.out.println("Failed to type keys: "+e.getMessage());
}
}
}
你可以尝试创建一个已经通过身份验证的配置文件,然后切换到它()@AndrianDurlestean这似乎是一个不同的建议,所以你应该将其作为自己的单独答案发布。我不知道为什么这两次被否决。我做到了这一点,它对我来说毫无问题。我最终使用了Firefox中显示的AutoAuth。但是我不知道如何用Chrome解决这个问题。你可以在使用任何浏览器时使用和处理身份验证窗口。目前我使用Browsermob代理来解决这个问题。