HTTP Basic Auth via URL in Firefox does not work?

后端 未结 8 1983
悲&欢浪女
悲&欢浪女 2020-11-30 02:18

I know that normally you can login to sites that require HTTP basic authentication with Selenium by passing the username and password in the URL, e.g.:

sele         


        
相关标签:
8条回答
  • 2020-11-30 02:51

    You could try to manipulate the headers directly like this:

    First when you start, you have to enable Selenium ti manipulate headers:

    selenium.start("addCustomRequestHeader=true");
    

    Then you have to use some basic encoding and header manipulation like this:

        String authHeader = "";
        try {
        BASE64Encoder coder = new BASE64Encoder();
        authHeader = coder.encode("developers:Str492ight".getBytes());
        }
        catch (Exception e)
        {
        e.printStackTrace();
        }
        setUpSelenium();
        startSelenium();
        selenium.addCustomRequestHeader("Authorization", "Basic " + authHeader);
        selenium.open("/");
        selenium.waitForPageToLoad("10000");
    

    The space after Basic is necessary. This is how a basic HTTP authentication header looks like..

    Further more you could use some Http Watchers to see if the request contains your auth request.

    Either use Wireshark, or better is Fiddler or Charles Proxy.

    Hope that helped. Gergely.

    0 讨论(0)
  • 2020-11-30 02:55

    Add a slash after the context root:

    Instead of: selenium.open("http://myusername:myuserpassword@mydomain.com/mypath");

    use: selenium.open("http://myusername:myuserpassword@mydomain.com/mypath/");

    It makes all the difference of the world adding the slash at the end of the context root. Without the slash, the popup opens, with the slash it gets authenticated as expected.

    Note, that this is not a selenium bug or whatnot, but a firefox thing. You can use your command line as well to see for yourself:

     C:\Program Files\Mozilla Firefox>firefox http://myusername:myuserpassword@mydomain.com/mypath/
    

    For me, it works even without settings the networks uris:

    FirefoxProfile profile = new FirefoxProfile();
    //profile.setPreference("network.automatic-ntlm-auth.trusted-uris", "mydomain.com");
    //profile.setPreference("network.negotiate-auth.trusteduris", "mydomain.com");
    
    WebDriver driver = new FirefoxDriver(profile);
    
    driver.navigate().to("http://myusername:myuserpassword@mydomain.com/mypath/");
    


    versions
    Firefox 19.0,
    selenium-java 2.31.0

    0 讨论(0)
提交回复
热议问题