【Selenium专题】WebDriver启动Chrome浏览器(

系统 1979 0

官方API

Constructor Summary

ChromeDriver()

Creates a new ChromeDriver using the default server configuration.

ChromeDriver(ChromeDriverService service)

Creates a new ChromeDriver instance.The service will be started along with the driver, and shutdown upon calling RemoteWebDriver.quit() .

ChromeDriver(ChromeOptions options)

Creates a new ChromeDriver instance with the specified options.

ChromeDriver(ChromeDriverService service, ChromeOptions options)

Creates a new ChromeDriver instance with the specified options. The service will be started along with the driver, and shutdown upon calling RemoteWebDriver.quit() .

ChromeDriver(Capabilities capabilities)

Deprecated . Use ChromeDriver(ChromeOptions) instead.

ChromeDriver(ChromeDriverService service, Capabilities capabilities)

Deprecated . Use ChromeDriver(ChromeDriverService, ChromeOptions)

注:chrome浏览器实例化现今只适用前四种,后两种已作废不用。

实例代码

  • 按照默认配置启动chrome  
      
        public
      
      
        static
      
      
        void
      
      
         main(String[] args) {

        String chromebin 
      
      = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
      
        ;//chrome启动文件路径

        String chromedriver 
      
      = "E:/**/**/**/chromedriver.exe"
      
        ;//chromedriver文件路径

    

        
      
      
        /*
      
      
         设定 chrome启动文件的位置, 若未设定则取默认安装目录的 chrome 
      
      
        */
      
      
        

        System.setProperty(
      
      "webdriver.chrome.bin"
      
        , chromebin);

        
      
      
        /*
      
      
         设定 chrome webdirver 的位置 ,若未设定则从path变量读取
      
      
        */
      
      
        

        System.setProperty(
      
      "webdriver.chrome.driver"
      
        , chromedriver);

        

        
        
          WebDriver driver 
        
      
      
        = 
        
          new
        
      
      
        
           ChromeDriver();
        
        

        driver.get(
      
      "http://www.baidu.com"
      
        );
      
    
      
                driver.close();//关闭浏览器
        
driver.quit();//退出浏览器
      
        }
      
    

 

  • 使用服务管理chrome
      
        public
      
      
        static
      
      
        void
      
      
         main(String[] args) {

        String chromedriver 
      
      = "E:/**/**/**/chromedriver.exe"
      
        ;

    


        
           ChromeDriverService service 
        
      
      
        = 
        
          new
        
        
           ChromeDriverService.Builder() .usingDriverExecutable(
        
        
          new
        
        
           File(chromedriver)) .usingAnyFreePort().build(); 
        
        
           WebDriver driver 
        
        = 
        
          new
        
      
      
        
           ChromeDriver(service);
        
        

        driver.get(
      
      "http://www.baidu.com"
      
        );

        driver.quit();

    }
      
    
  • 自定义配置启动chrome  
      
        import
      
      
         java.io.File;




      
      
        import
      
      
         org.openqa.selenium.WebDriver;


      
      
        import
      
      
         org.openqa.selenium.chrome.ChromeDriver;


      
      
        import
      
      
         org.openqa.selenium.chrome.ChromeOptions;




      
      
        public
      
      
        class
      
      
         test {

    
      
      
        public
      
      
        static
      
      
        void
      
      
         main(String[] args) {

        String chromebin 
      
      = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
      
        ;

        String chromedriver 
      
      = "E:/**/**/**/chromedriver.exe"
      
        ;

    

        
      
      
        /*
      
      
         设定 chrome启动文件的位置, 若未设定则取默认安装目录的 chrome 
      
      
        */
      
      
        

        System.setProperty(
      
      "webdriver.chrome.bin"
      
        , chromebin);

        
      
      
        /*
      
      
         设定 chrome webdirver 的位置 ,若未设定则从path变量读取
      
      
        */
      
      
        

        System.setProperty(
      
      "webdriver.chrome.driver"
      
        , chromedriver);

        

        
        
          WebDriver driver 
        
      
      
        = 
        
          new
        
      
      
        
           ChromeDriver(setChromeOptions());
        
        

        driver.get(
      
      "http://www.baidu.com"
      
        );
        
driver.close();
//关闭浏览器
driver.quit();/ /退出浏览器
}
      
        
          
            /**
          
          
             * 设置 Chrome 浏览器的启动配置 * 
          
          
            @return
          
          
             ChromeOptions Chrome 参数设置 
          
          
            */
          
          
            
              public
            
            
              static
            
            
               ChromeOptions setChromeOptions(){ ChromeOptions options 
            
            = 
            
              new
            
            
               ChromeOptions(); 
            
            
              /*
            
            
               * 设置参数 * --start-maximized 浏览器最大化 * test-type 忽略认证错误警示--ignore-certificate-errors * 
            
            
              */
            
            
               options.addArguments(
            
            "--start-maximized"
            
              ); options.addArguments(
            
            "test-type"
            
              ); 
            
            
              /*
            
            
               * 加载插件 * files\\youtube.crx 代表查件文件路径 * 
            
            
              */
            
            
               File file 
            
            = 
            
              new
            
             File ("files\\youtube.crx"
            
              ); options.addExtensions(file); 
            
            
              return
            
          
          
            
               options; }
            
             }
          
        
      
    

 

  • 自定义配置,使用服务启动chrome  
      
        import
      
      
         java.io.File;




      
      
        import
      
      
         org.openqa.selenium.WebDriver;


      
      
        import
      
      
         org.openqa.selenium.chrome.ChromeDriver;


      
      
        import
      
      
         org.openqa.selenium.chrome.ChromeDriverService;


      
      
        import
      
      
         org.openqa.selenium.chrome.ChromeOptions;




      
      
        public
      
      
        class
      
      
         test {



    
      
      
        public
      
      
        static
      
      
        void
      
      
         main(String[] args) {

        String chromedriver 
      
      = "E:/**/**/**/chromedriver.exe"
      
        ;

    


        
           ChromeDriverService service 
        
      
      
        = 
        
          new
        
        
           ChromeDriverService.Builder() .usingDriverExecutable(
        
        
          new
        
        
           File(chromedriver)) .usingAnyFreePort().build(); 
        
        
           WebDriver driver 
        
        = 
        
          new
        
      
      
        
           ChromeDriver(service,setChromeOptions());
        
        

        driver.get(
      
      "http://www.baidu.com"
      
        );

        driver.quit();

    }

    

    
      
      
        /**
      
      
        

     * 设置 Chrome 浏览器的启动配置

     * 
      
      
        @return
      
      
         ChromeOptions Chrome 参数设置

     
      
      
        */
      
      
        
          public
        
        
          static
        
        
           ChromeOptions setChromeOptions(){ ChromeOptions options 
        
        = 
        
          new
        
        
           ChromeOptions(); 
        
        
          /*
        
        
           * 设置参数 * --start-maximized 浏览器最大化 * test-type 忽略认证错误警示--ignore-certificate-errors * 
        
        
          */
        
        
           options.addArguments(
        
        "--start-maximized"
        
          ); options.addArguments(
        
        "test-type"
        
          ); 
        
        
          /*
        
        
           * 加载插件 * files\\youtube.crx 代表查件文件路径 * 
        
        
          */
        
        
           File file 
        
        = 
        
          new
        
         File ("files\\youtube.crx"
        
          ); options.addExtensions(file); 
        
        
          return
        
      
      
        
           options; }
        
            

}
      
    

 

注意 :为避免路径问题,chrome浏览器建议安装在默认路径下

 

【Selenium专题】WebDriver启动Chrome浏览器(二)


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论