栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

一、启动谷歌浏览器

      安装的浏览器版本:68.0.3440.75

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

步骤:

1、创建maven工程

2、引入selenium框架


      org.seleniumhq.selenium
      selenium-java
      3.6.0
   

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/344814.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号