5

ほとんどのページが通常 HTTP 経由で使用される Web サイトを持っていますが、他のいくつかのページは HTTPS 経由でのみ使用できます。サイトは基本認証によって保護されています (認証情報は HTTP ページと HTTPS ページで同じです)。

ブラウザー (FF または Chrome) で任意の HTTP ページを開き、HTTPS ページにつながるリンクをクリックすると、基本的な認証資格情報を要求するアラートがブラウザーに表示されます。

Webdriver (FF または Chrome) にも同じ問題があります。HTTPS ページ
にアクセスhttp://username:password@some_domain.comしてリンクをクリックすると、基本認証資格情報を要求するブラウザ警告ウィンドウが表示されます。Selenium は、HTTP ページに入力された資格情報を「記憶」しません。

Webdriver でこの一連のアクションを実行するにはどうすればよいですか? もしそれが不可能なら、あなたは何をアドバイスできますか?

4

2 に答える 2

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);
于 2013-06-03T15:58:41.410 に答える
1

これまでに思いついた最善の解決策は、タイムアウトを処理する新しいスレッドを作成することです。WebDriver は FF やその他の特定のブラウザーでは制御を返さないため、ロボットを使用して資格情報を入力し、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());
        }
    }
}
于 2013-08-02T20:04:15.727 に答える