Selenium + Webdriver 学习(二) 各种浏览器简单操作

本文介绍了如何使用Java代码打开并操作FF、IE、Chrome三种常见浏览器的基本方法,并提供了解决IE浏览器异常的方法。同时,文章还详细解释了关闭浏览器、获取当前页面URL、窗口句柄等操作,并对操作过程中可能出现的异常进行了说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

打开一个测试浏览器

对浏览器进行操作首先需要打开一个浏览器,接下来才能对浏览器进行操作。但要注意的是,因为Chrome Driver是Chromium项目自己支持和维护的,所以你必需另外下载安装Chrome Driver,详细介绍查下他们的wiki

以下是对FF\ IE(7.8.9.10)\chrome 浏览器的例子

Java代码:

  1.   
  2. /*** This class  open browser 
  3. * @author annie.wang   
  4. */  
  5. package com.annie.test;  
  6.   
  7. import java.io.File;  
  8.   
  9. import org.openqa.selenium.WebDriver;  
  10. import org.openqa.selenium.chrome.ChromeDriver;  
  11. import org.openqa.selenium.firefox.FirefoxBinary;  
  12. import org.openqa.selenium.firefox.FirefoxDriver;  
  13. import org.openqa.selenium.ie.InternetExplorerDriver;  
  14. import org.openqa.selenium.remote.DesiredCapabilities;  
  15.   
  16. public class OpenBrowser {  
  17.   
  18.     /** 
  19.      * @param args 
  20.      */  
  21.     public static void main(String[] args) {  
  22.         // TODO Auto-generated method stub   
  23.           
  24.           /** default open firefox  ***/   
  25.          WebDriver diver = new FirefoxDriver();    
  26.         System.out.println("success open the Firefox01");  
  27.             
  28.         //打开指定路径的firefox,方法1   
  29.         System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");     
  30.         WebDriver dr = new FirefoxDriver();    
  31.         System.out.println("success open the Firefox02");  
  32.             
  33.         //打开指定路径的firefox,方法2   
  34.          File pathToFirefoxBinary = new File("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");      
  35.         FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);      
  36.         WebDriver driver1 = new FirefoxDriver(firefoxbin,null);    
  37.         System.out.println("success open the Firefox03");  
  38.         /** open IE browser    */  
  39.         DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();  
  40.         ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);  
  41.         WebDriver oWebDriver = new InternetExplorerDriver(ieCapabilities);//    Setting the IE browser security   
  42.        System.setProperty("webdriver.ie.driver","C:\\Windows\\IEDriverServer.exe");  
  43.        oWebDriver.get("http://www.google.com");  
  44.        System.out.println("success open the IE");  
  45.         /**open chrome browser  */  
  46.        System.setProperty("webdriver.chrome.driver","C:\\Windows\\chromedriver.exe");      
  47.        // System.setProperty("webdriver.chrome.bin","C:\\Windows\\chromedriver.exe");    
  48.         WebDriver chdr=new ChromeDriver();   
  49.          /** close the browser   1.method :quit 
  50.           * driver.quit();  
  51.           * close the browser   2.method :close  
  52.           * driver.close();   
  53.           */  
  54.           
  55.         
  56.         /**得到当前页面url  
  57.          * 返回当前浏览器的窗口句柄 */  
  58.         /* String currentUrl = driver.getCurrentUrl();   
  59.    
  60.         //输出title和currenturl   
  61.         System.out.println("页面title:"+title+"\n"+"当前URL"+currentUrl);  
  62.       //  ((JavascriptExecutor)driver).executeScript("alert(\"hello,this is a alert!\")");  
  63.           
  64.         String windowHandle=driver.getWindowHandle() ;  
  65.         System.out.println("当前窗口句柄:"+windowHandle+"\n"+windowHandle);  
  66.         //String windowHandles=driver.getWindowHandles(Set<STRING>); // 返回所有浏览器的窗口句柄 
  67.        // String pageSource=driver.getPageSource()  ;  //返回当前页面的源码 
  68.        // System.out.println(pageSource+"\n"+pageSource);  
  69.         */  
  70.     }    
  71.     
  72. }    
  73. </STRING>  
/*** This class  open browser
* @author annie.wang  
*/
package com.annie.test;

import java.io.File;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class OpenBrowser {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		  /** default open firefox  ***/ 
         WebDriver diver = new FirefoxDriver();  
        System.out.println("success open the Firefox01");
          
        //打开指定路径的firefox,方法1
        System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");   
        WebDriver dr = new FirefoxDriver();  
        System.out.println("success open the Firefox02");
          
        //打开指定路径的firefox,方法2
         File pathToFirefoxBinary = new File("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");    
        FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);    
        WebDriver driver1 = new FirefoxDriver(firefoxbin,null);  
        System.out.println("success open the Firefox03");
        /** open IE browser    */
		DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
		ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
		WebDriver oWebDriver = new InternetExplorerDriver(ieCapabilities);// 	Setting the IE browser security
	   System.setProperty("webdriver.ie.driver","C:\\Windows\\IEDriverServer.exe");
	   oWebDriver.get("http://www.google.com");
	   System.out.println("success open the IE");
	    /**open chrome browser  */
       System.setProperty("webdriver.chrome.driver","C:\\Windows\\chromedriver.exe");    
       // System.setProperty("webdriver.chrome.bin","C:\\Windows\\chromedriver.exe"); 
        WebDriver chdr=new ChromeDriver(); 
         /** close the browser   1.method :quit
          * driver.quit(); 
          * close the browser   2.method :close 
          * driver.close();  
          */
        
      
        /**得到当前页面url 
         * 返回当前浏览器的窗口句柄 */
        /* String currentUrl = driver.getCurrentUrl();  
  
        //输出title和currenturl  
        System.out.println("页面title:"+title+"\n"+"当前URL"+currentUrl); 
      //  ((JavascriptExecutor)driver).executeScript("alert(\"hello,this is a alert!\")"); 
         
        String windowHandle=driver.getWindowHandle() ; 
        System.out.println("当前窗口句柄:"+windowHandle+"\n"+windowHandle); 
        //String windowHandles=driver.getWindowHandles(Set); // 返回所有浏览器的窗口句柄
       // String pageSource=driver.getPageSource()  ;  //返回当前页面的源码
       // System.out.println(pageSource+"\n"+pageSource); 
        */
    }  
  
}  

IE 中 尤其注意,可能会报以下异常:

 

这个异常是由于IE浏览器的安全权限和zoom造成的,以下3个步骤方可解决

1. 加入此代码片段:

  1. DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();  
  2. ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);  
  3. WebDriver oWebDriver = new InternetExplorerDriver(ieCapabilities);//Setting the IE browser security  
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
WebDriver oWebDriver = new InternetExplorerDriver(ieCapabilities);//Setting the IE browser security

2. 设置Internet Option Enable 选项 勾掉

3. 设置zoom 为100%

 

运行成功,注意IE9下有异常 。尚未有解决方案,但是不影响IE9 测试功能

小结

从上面代码可以看出操作浏览器的主要方法都来自org.openqa.selenium.WebDriver这个接口中。看了一下源代码这些方法都 是在org.openqa.selenium.remote.RemoteWebDriver这个类中实现的,然后不同浏览的driver类继承 RemoteWebDriver。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值