How to set Proxy Authentication in seleniumWebdriver for Chrome Browser

后端 未结 5 520
忘了有多久
忘了有多久 2021-01-05 17:58

I\'m trying to Automate a web application selenium 2.0 [webdriver+java].The web application is currently deployed in our UAT servers on our local network.My test cases are e

相关标签:
5条回答
  • 2021-01-05 18:16

    This code (from Avishka Perera's answer) does not work for me:

        proxy.setSocksUsername("avishka");
        proxy.setSocksPassword("12345678");
    

    The username and password set in this way do not take effect for the http/https proxy - the Proxy Authentication box still popped up.

    I'm using Selenium java 3.141.0, ChromeDriver 2.33 and chrome 70. What works for me is to follow Mike's answer here Selenium using Python: enter/provide http proxy password for firefox . Create the zip file, then add the extension like this:

    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addExtensions(new File("src/test/resources/proxy.zip"));
    WebDriver driver = new ChromeDriver(chromeOptions);
    

    One catch is that the above code will run into error if you set "--headless" argument because chrome in headless mode cannot have extension (Is it possible to run Google Chrome in headless mode with extensions?). If your Chrome runs in Docker container and cannot show the UI, then to get this solution work, you'll need to run with Xvfb instead of in headless mode.

    0 讨论(0)
  • 2021-01-05 18:28

    The approach that worked perfectly fine for me is by using AutoIT.

    Install autoIT and prepare a simple script as shown in the picture attached and execute the script file from your testscript using Runtime.getRuntime().exec("\YOUR_SCRIPT.exe") before navigating to the baseURL.

    0 讨论(0)
  • 2021-01-05 18:31
        public class DriverClass {
    
        private String baseUrl;
        private String driverPath;
        private String driverName;
        private static WebDriver driver;
        private static DriverClass driverClass;
    
        public DriverClass() {
            try {
                baseUrl = "http://192.168.0.10:8282/ess";
                driverPath = "E:\\Work_Folder\\SelTools\\chromedriver.exe";
                driverName = "webdriver.chrome.driver";
                System.setProperty(driverName, driverPath);
    
                Proxy proxy = new org.openqa.selenium.Proxy();
                proxy.setSslProxy("192.168.0.200" + ":" + 3128);
                proxy.setFtpProxy("192.168.0.200" + ":" + 3128);
                proxy.setSocksUsername("avishka");
                proxy.setSocksPassword("12345678");
    
                DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
                desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);
    
    
                driver = new ChromeDriver(desiredCapabilities);
    
    
                driver.get(baseUrl);
    
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    The proxy setting has been added with desired capabilities to pass values to proxy authentication,worked finally

    0 讨论(0)
  • 2021-01-05 18:31

    Simple method to add authenticated proxy using selenium wire in Both firefox and chrome

    In python

    Step:1

    pip3 install selenium-wire
    

    Step:2

    from seleniumwire import webdriver
    from selenium import webdriver
    

    step:3

    Add proxy in below-mensioned format

    proxy= "username:password@ip:port"
            options = {'proxy': {'http': proxy, 'https': proxy, 'no_proxy': 'localhost,127.0.0.1,dev_server:8080'}}
    

    step:4 pass proxy as an argument

    CHROME

    driver = webdriver.Chrome(options=chrome_options, executable_path="path of chrome driver", seleniumwire_options=options)
    

    Firefox

    driver = webdriver.Firefox(seleniumwire_options=options, executable_path="path of firefox driver", options=firefox_options)
    

    step:5 Verify proxy applied by requesting the url https://whatismyipaddress.com/

    time.sleep(20)
    driver.get("https://whatismyipaddress.com/")
    

    Note: But selenium log shows it runs in without proxy because we are using an external package to apply proxy.

    0 讨论(0)
  • 2021-01-05 18:32

    You can do via MultiPass for HTTP basic authentication

    Download the extension from
    https://chrome.google.com/webstore/detail/multipass-for-http-basic/enhldmjbphoeibbpdhmjkchohnidgnah

    Download the extension as crx. You can get it as crx from chrome-extension-downloader

    After that the config is simple.

    import java.io.File;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    
    /**
     *
     * @author Phystem
     */
    public class ChromeAuthTest {
    
        WebDriver driver;
    
        public ChromeAuthTest() {
            System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
        }
    
        private void initDriver() {
            ChromeOptions cOptions = new ChromeOptions();
            cOptions.addExtensions(new File("MultiPass-for-HTTP-basic-authentication_v.crx"));
            driver = new ChromeDriver(cOptions);
            configureAuth(
                    "https://the-internet.herokuapp.com/basic_auth",
                    "admin",
                    "admin");
        }
    
        private void configureAuth(String url, String username, String password) {
            driver.get("chrome-extension://enhldmjbphoeibbpdhmjkchohnidgnah/options.html");
            driver.findElement(By.id("url")).sendKeys(url);
            driver.findElement(By.id("username")).sendKeys(username);
            driver.findElement(By.id("password")).sendKeys(password);
            driver.findElement(By.className("credential-form-submit")).click();
        }
    
        public void doTest() {
            initDriver();
            driver.get("https://the-internet.herokuapp.com/basic_auth");
            System.out.println(driver.getTitle());
            driver.quit();
        }
    
        public static void main(String[] args) {
            new ChromeAuthTest().doTest();
        }
    }
    

    I have used a sample site for testing.

    Provide your url,username and password in the configure Auth function and try

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