selenium webdriver学习(二)————对浏览器的简单操作

selenium webdriver对浏览器的简单操作

打开一个测试浏览器

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

Java代码
  1. import java.io.File; 
  2.  
  3. import org.openqa.selenium.WebDriver; 
  4. import org.openqa.selenium.firefox.FirefoxBinary; 
  5. import org.openqa.selenium.firefox.FirefoxDriver; 
  6. import org.openqa.selenium.ie.InternetExplorerDriver; 
  7.  
  8.  
  9. public class OpenBrowsers { 
  10.  
  11.      
  12.     public staticvoid main(String[] args) { 
  13.         //打开默认路径的firefox 
  14.         WebDriver diver = new FirefoxDriver(); 
  15.          
  16.         //打开指定路径的firefox,方法1 
  17.         System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");  
  18.         WebDriver dr = new FirefoxDriver(); 
  19.          
  20.         //打开指定路径的firefox,方法2 
  21.         File pathToFirefoxBinary = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe");   
  22.         FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);   
  23.         WebDriver driver1 = new FirefoxDriver(firefoxbin,null); 
  24.          
  25.         //打开ie 
  26.         WebDriver ie_driver = new InternetExplorerDriver(); 
  27.          
  28.         //打开chrome 
  29.         System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe"); 
  30.         System.setProperty("webdriver.chrome.bin"
  31.                                              "C:\\Documents and Settings\\gongjf\\Local Settings" 
  32.                                              +"\\Application Data\\Google\\Chrome\\Application\\chrome.exe");        
  33.          
  34.     } 
  35.  
import java.io.File;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;


public class OpenBrowsers {

	
	public static void main(String[] args) {
		//打开默认路径的firefox
		WebDriver diver = new FirefoxDriver();
		
		//打开指定路径的firefox,方法1
		System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe"); 
		WebDriver dr = new FirefoxDriver();
		
		//打开指定路径的firefox,方法2
		File pathToFirefoxBinary = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe");  
		FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);  
		WebDriver driver1 = new FirefoxDriver(firefoxbin,null);
		
		//打开ie
		WebDriver ie_driver = new InternetExplorerDriver();
		
		//打开chrome
		System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
		System.setProperty("webdriver.chrome.bin",
                                             "C:\\Documents and Settings\\gongjf\\Local Settings"
                                             +"\\Application Data\\Google\\Chrome\\Application\\chrome.exe");		
		
	}

}

打开指定路经ie和chrome方法和ff一样。

打开1个具体的url

打开一个浏览器后,我们需要跳转到特定的url下,看下面代码:

Java代码
  1. import org.openqa.selenium.WebDriver; 
  2. import org.openqa.selenium.firefox.FirefoxDriver; 
  3.  
  4. public class OpenUrl { 
  5.     public staticvoid main(String []args){ 
  6.         String url = "http://www.51.com"
  7.         WebDriver driver = new FirefoxDriver(); 
  8.          
  9.         //用get方法 
  10.         driver.get(url); 
  11.          
  12.         //用navigate方法,然后再调用to方法 
  13.         driver.navigate().to(url); 
  14.     } 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class OpenUrl {
	public static void main(String []args){
		String url = "http://www.51.com";
		WebDriver driver = new FirefoxDriver();
		
		//用get方法
		driver.get(url);
		
		//用navigate方法,然后再调用to方法
		driver.navigate().to(url);
	}
}

 

如何关闭浏览器

测试完成后,需要关闭浏览器

Java代码
  1. import org.openqa.selenium.WebDriver; 
  2. import org.openqa.selenium.firefox.FirefoxDriver; 
  3.  
  4. public class CloseBrowser { 
  5.     public staticvoid main(String []args){ 
  6.         String url = "http://www.51.com"
  7.         WebDriver driver = new FirefoxDriver(); 
  8.          
  9.         driver.get(url); 
  10.          
  11.         //用quit方法 
  12.         driver.quit(); 
  13.          
  14.         //用close方法   
  15.         driver.close(); 
  16.         } 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class CloseBrowser {
	public static void main(String []args){
		String url = "http://www.51.com";
		WebDriver driver = new FirefoxDriver();
		
		driver.get(url);
		
		//用quit方法
		driver.quit();
		
		//用close方法	
		driver.close();
		}
}

如何返回当前页面的url和title

有时候我们需要返回当前页面的url或者title做一些验证性的操作等。代码如下:

Java代码
  1. import org.openqa.selenium.WebDriver; 
  2. import org.openqa.selenium.firefox.FirefoxDriver; 
  3.  
  4. public class GetUrlAndTitle { 
  5.     public staticvoid main(String []args){ 
  6.         String url = "http://www.51.com"
  7.         WebDriver driver = new FirefoxDriver(); 
  8.          
  9.         driver.get(url); 
  10.          
  11.                 //得到title 
  12.         String title = driver.getTitle(); 
  13.  
  14.                 //得到当前页面url 
  15.         String currentUrl = driver.getCurrentUrl(); 
  16.          
  17.                 //输出title和currenturl 
  18.         System.out.println(title+"\n"+currentUrl); 
  19.          
  20.         } 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GetUrlAndTitle {
	public static void main(String []args){
		String url = "http://www.51.com";
		WebDriver driver = new FirefoxDriver();
		
		driver.get(url);
		
                //得到title
		String title = driver.getTitle();

                //得到当前页面url
		String currentUrl = driver.getCurrentUrl();
		
                //输出title和currenturl
		System.out.println(title+"\n"+currentUrl);
		
		}
}

 

其他方法

  • getWindowHandle()    返回当前的浏览器的窗口句柄
  • getWindowHandles()  返回当前的浏览器的所有窗口句柄
  • getPageSource()         返回当前页面的源码


小结

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值