selenium webdriver学习(二)————对浏览器的简单操做(转载JARVI)

selenium webdriver对浏览器的简单操做java

 

打开一个测试浏览器

对浏览器进行操做首先须要打开一个浏览器,接下来才能对浏览器进行操做。但要注意的是,由于Chrome Driver是Chromium 项目本身支持和维护的,因此你必需另外下载安装Chrome Driver,详细介绍查下他们的wiki web

 

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

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

 

打开1个具体的url

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

Java代码   收藏代码
  1. import org.openqa.selenium.WebDriver;  
  2. import org.openqa.selenium.firefox.FirefoxDriver;  
  3.   
  4. public class OpenUrl {  
  5.     public static void 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.     }  
  15. }  

 

 

如何关闭浏览器

测试完成后,须要关闭浏览器学习

Java代码   收藏代码
  1. import org.openqa.selenium.WebDriver;  
  2. import org.openqa.selenium.firefox.FirefoxDriver;  
  3.   
  4. public class CloseBrowser {  
  5.     public static void 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.         }  
  17. }  

 

如何返回当前页面的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 static void 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.         }  
  21. }  

 

 

其余方法

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

 

小结

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

相关文章
相关标签/搜索