Selenium - Basic Authentication via url

后端 未结 5 2167
时光说笑
时光说笑 2020-11-22 01:02

In my Selenium-Test (with chromedriver-2.24) I\'m trying to access my webpage via basic authentication with the following statement:



        
5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-22 01:13

    There were some updates in this link as :

    Chromium Issue 435547 Drop support for embedded credentials in subresource requests. (removed)

    We should block requests for subresources that contain embedded credentials (e.g. "http://ima_user:hunter2@example.com/yay.tiff"). Such resources would be handled as network errors.

    However, Basic Authentication functionality still works with Selenium 3.4.0, geckodriver v0.18.0, chromedriver v2.31.488763, Google Chrome 60.x and Mozilla Firefox 53.0 through Selenium-Java bindings.

    Here is the example code which tries to open the URL http://the-internet.herokuapp.com/basic_auth with a valid set of credentials and it works.

    Firefox:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class BasicAuthentication_FF 
    {
        public static void main(String[] args) 
        {
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            WebDriver driver =  new FirefoxDriver();
            driver.navigate().to("http://admin:admin@the-internet.herokuapp.com/basic_auth");
        }
    }
    

    Chrome:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    
    public class BasicAuthentication_Chrome 
    {
        public static void main(String[] args) 
        {
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.addArguments("disable-infobars");
            options.addArguments("--disable-extensions");
            WebDriver driver =  new ChromeDriver(options);
            driver.navigate().to("http://admin:admin@the-internet.herokuapp.com/basic_auth");
        }
    }
    

提交回复
热议问题