如何通过窗口询问单击链接从HTTP重定向到HTTPS时出现的基本身份验证凭据?

时间:2013-06-03 15:15:12

标签: selenium webdriver selenium-webdriver basic-authentication

我有一个网站,大多数网页通常通过HTTP使用,但其他一些网页只能通过HTTPS使用。站点受基本身份验证保护(HTTP和HTTPS页面的凭据相同)。

当我在浏览器中打开任何HTTP页面(FF或Chrome)并单击指向HTTPS页面的链接时,浏览器会显示要求提供基本身份验证凭据的警报。

我对Webdriver(FF或Chrome)也有同样的问题:
当我访问http://username:password@some_domain.com并单击指向HTTPS页面的链接时,会出现要求提供基本身份验证凭据的浏览器警报窗口。 Selenium不会“记住”为HTTP页面输入的凭据。

如何使用Webdriver执行这一系列操作?如果不可能你有什么建议吗?

2 个答案:

答案 0 :(得分:3)

 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);

hostname是您的URL(example.com),然后尝试

Driver.Navigate().GoToUrl(http://user:password@example.com);

答案 1 :(得分:1)

到目前为止,我能够提出的最佳解决方案是创建一个处理超时的新线程。由于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());
        }
    }
}