Selenium HtmlUnitDriver多浏览器的支持

1、HtmlUnitDriver
  WebDriver包括一个基于HtmlUnit的无界面实现,称为HtmlUnitDriver,即使用HtmlUnit时并不会打开真实的浏览器,而是在内存中执行代码,因此运行速度很快,但是对JavaScript的支持不够好,当页面上有复杂的JavaScript元素时,经常捕捉不到。
  eclipse测试例子如下:
  WebDriver dr = new HtmlUnitDriver();
  dr.get(“http://www.baidu.com“);
  WebElement element = dr.findElement(By.name(“wd”));
  element.sendKeys(“webdriver”);
  element.submit();
  Thread.sleep(5000);
  System.out.println(“page title is:”+dr.getTitle());
  运行成功时控制台会打印 百度搜索页面标题“page title is:webdriver_百度搜索”。
   2、Firefox
  WebDriver实现了FireFoxDriver,无需用户下载FireFoxDriver。
  优点:FireFoxDriver对页面的自动化 测试支持得比较好,很直观地模拟页面的操作,对JavaScript的支持也非常完善,基本上页面上做的所有操作FireFox Driver都可以模拟。
  缺点:启动很慢,运行也比较慢,不过,启动之后Webdriver的操作速度虽然不快但还是可以接受的,建议不要频繁启动停止FireFoxDriver。
  使用Firefox浏览器只需要设置WebDriver driver = new FirefoxDriver(),前提是你的Firefox被安装在默认的位置。
   操作系统  Firefox默认安装位置
   Linux  firefox (found using “which”)
  Mac /Applications/Firefox.app/Contents/MacOS/firefox
   Windows  %PROGRAMFILES%\Mozilla Firefox\firefox.exe
  如果你的FireFox没有被安装在指定的位置,可以设置“webdriver.firefox.bin”来指定它的位置, java代码如下:
  System.setProperty(“webdriver.firefox.bin”,”thelocation of Firefox”);
  eclipse测试例子如下:
  System.setProperty(“webdriver.firefox.bin”,”D:\Mozilla Firefox\firefox.exe”);
  WebDriver dr = new FirefoxDriver();
  dr.get(“http://www.baidu.com“);
  WebElement element = dr.findElement(By.name(“wd”));
  element.sendKeys(“webdriver”);
  element.submit();
  Thread.sleep(5000);
  System.out.println(“page title is:”+dr.getTitle());
      Selenium-java3.0以上,仍然要用驱动的,见下面的代码
   	//无法正常启动,添加以下代码试试(网上是说selelium3.0以上的就要指定驱动了),注意大小写哦
        //驱动下载地址:https://github.com/mozilla/geckodriver/releases,请选择对应的版本
        System.setProperty("webdriver.geck1o.driver", "D:\\Selenium\\driver\\geckodriver-v0.16.1.exe");
        //如果以上还不行,那再添加下面这句吧
        //System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
        driver = new FirefoxDriver();
 
   3、Chrome
  webdriver没有实现chromedriver,要使用chrome浏览器需要自己下载chromedriver.exe(下载地址:http://code.google.com/p/chromedriver/downloads/list),这个程序是由Chrome团队提供的,你可以看做它是链接WebDriver和Chrome浏览器的桥梁。
  eclipse例子如下:
  System.setProperty(“webdriver.chrome.driver”,”D:\chromedriver\chromedriver.exe”); //指定chromedriver的路径
  System.setProperty(“webdriver.chrome.bin”,”C:\Documents and Settings\gongjf\Local Settings\Application Data\Google\Chrome\Application\chrome.exe”); //chrome没有安装在默认路径时,指定chrome.exe的路径
  WebDriver driver = new ChromeDriver();
  driver.get(“http://www.baidu.com“);
       运行时,会出现一些参数样的,需要将其去掉,见以下代码
        //指定chrome驱动路径,驱动下载地址:http://chromedriver.storage.googleapis.com/index.html,请选择对应的版本
        System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\driver\\chromedriver-2.29.exe");

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability("chrome.switches", Arrays.asList("--ignore-certificate-errors"));
        ChromeOptions chromeOptions = new ChromeOptions();
        //chromeOptions.addArguments("ignore-certifcate-errors");
        chromeOptions.addArguments("test-type");
        capabilities.setCapability("chrome.binary","C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
        capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
        driver = new ChromeDriver(capabilities);//定义driver

   4、IE
  webdriver要使用IE浏览器需要下载InternetExplorerDriver.exe(下载地址:http://code.google.com/p/selenium/downloads/list),根据浏览器的版本下载32位或者64位的driver。
  注意:需要将IE浏览器各个区域的保护模式设置的一样,要么全勾选,要么全不勾选,工具–Internet选项–安全。还需要将页面的缩放比例设置为100%
  优点:直观地模拟用户的实际操作,对JavaScript提供完善的支持。
  缺点:是所有浏览器中运行速度最慢的,并且只能在Windows下运行,对CSS以及XPATH的支持也不够好。
  System.setProperty(“webdriver.ie.driver”,”D:\iedriver\IEDriverServer.exe”); //设置IEDriverService.exe的路径;如果IE没有安装在默认目录,同样需要设置webdriver.ie.bin
  WebDriver driver = new InternetExplorerDriver();
  driver.get(“http://www.baidu.com“);
        目前试过IE11仍然不行,IE10可以,但是运行很慢,比如输入用户名和密码,每个字母都要好几秒,不知道为何?
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值