web自动化-1、启动主流浏览器

一、启动谷歌浏览器

      安装的浏览器版本:68.0.3440.75

      下载的谷歌浏览器启动需要的驱动:2.39版本  放到maven工程的src/test/resources目录下

步骤:

1、创建maven工程

2、引入selenium框架

<dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>3.6.0</version>
    </dependency>

3、编写web自动化脚本

    public static  void main(String[] arg){
//        引入chrome启动驱动
        System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
//        初始化驱动对象
        ChromeDriver driver=new ChromeDriver();
//        访问网页
        driver.get("http://www.baidu.com");
        
        
    }
    

chrome浏览器各版本 http://www.chromedownloads.net/chrome64win/
禁⽌⾕歌浏览器更新 https://jingyan.baidu.com/article/76a7e409f2137afc3b6e15be.html
System . setProperty ( "webdriver.chrome.driver" , "xxx" );
1 ChromeDriver 镜像 http://npm.taobao.org/mirrors/chromedriver/  
Selenium 镜像 http://npm.taobao.org/mirrors/selenium

    

二、火狐浏览器

      安装的浏览器版本:87.0 (64 位)

      下载的谷歌浏览器启动需要的驱动:0.25版本  放到maven工程的src/test/resources目录下

1、编写web自动化脚本:

    public static  void openFirefor(){
        //如果不是安装在默认路径,是自定义安装的,需加上如下设置
        System.setProperty("webdriver.firefox.bin", "G:\\firefox\\firefox.exe");
//        引入chrome启动驱动
        System.setProperty("webdriver.gecko.driver", "src/test/resources/geckodriver.exe");
//        初始化驱动对象
        driver=new FirefoxDriver();    
    }

2、遇到的问题:

2.1、Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: WIN10
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T15:28:36.4Z'

      原因以及解决方法:火狐浏览器没有安装在默认路径,是自定义安装的

                      解决办法: 指定firefox可执⾏⽂件路径:webdriver.firefox.bin

                    System.setProperty("webdriver.fierfox.bin", "G:\\firefox\\firefox.exe");

                    其中G:\\firefox\\firefox.exe指安装路径

2.2、Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information,            

      原因:无驱动包

      解决方法:往项⽬中添加⽕狐驱动包,并加载驱动的配置

        System.setProperty("webdriver.gecko.driver", "/src/test/resources/geckodriver.exe");

Firefox⾃动化相关⼯具链接
Change log https://raw.githubusercontent.com/SeleniumHQ/selenium/master/java/CHANGELOG
Firefox驱动 https://github.com/mozilla/geckodriver/releases/
Firefox各版本 http://ftp.mozilla.org/pub/firefox/releases/
Firefox驱动 镜像 https://npm.taobao.org/mirrors/geckodriver/

三、启动IE浏览器

1、遇到的问题:
1.1、Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.ie.driver system property; for more information,

      原因:无驱动包

      解决方法:往项⽬中添加ie驱动包,并加载驱动的配置,IE驱动版本与Selenium版本保持相同即可

       System.setProperty("webdriver.ie.driver", "src/test/resources/IEDriverServer.exe");

1.2、Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.

        原因:ie浏览器的保护模式问题

         解决方法:忽略浏览器保护模式的配置

//        给予浏览器一个额外的能力
        DesiredCapabilities capabilities=new DesiredCapabilities();
//        忽略浏览器的保护模式
        capabilities.setCapability("ignoreProtectedModeSettings", true);

       driver=new InternetExplorerDriver(capabilities);

1.3、Unexpected error launching Internet Explorer. Browser zoom level was set to 125%. It should be set to 100%

          原因:浏览器的缩放级别设置不一致

          解决方法: 忽略此设置

//        给予浏览器一个额外的能力
        DesiredCapabilities capabilities=new DesiredCapabilities();
//      忽略浏览器的大小设置
        capabilities.setCapability("ignoreZoomSetting", true);
//        初始化驱动对象
        driver=new InternetExplorerDriver(capabilities);   

2、编写web自动化代码:

public static  void openIEbrower(){
        
//        引入ie启动驱动
        System.setProperty("webdriver.ie.driver", "src/test/resources/IEDriverServer.exe");
//        给予浏览器一个额外的能力
        DesiredCapabilities capabilities=new DesiredCapabilities();
//        忽略浏览器的保护模式
        capabilities.setCapability("ignoreProtectedModeSettings", true);
//        忽略浏览器的大小设置
        capabilities.setCapability("ignoreZoomSetting", true);
//        初始化驱动对象
        driver=new InternetExplorerDriver(capabilities);    
    }

下载地址 http://www.seleniumhq.org/download/
建议下载版本:3.7.0 http://selenium-release.storage.googleapis.com/index.html?path=3.7/
IE驱动版本与Selenium版本保持相同即可 http://selenium-release.storage.googleapis.com/index.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值