selenium webdriver对浏览器的简单操作

打开一个测试浏览器

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

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

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

打开1个具体的url

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

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

如何关闭浏览器

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

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

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

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

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

其他方法

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

小结

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

04-25 22:32
查看更多