zoukankan      html  css  js  c++  java
  • 【Selenium WebDriver】浏览器操作篇:打开浏览器、打开URL、关闭浏览器、获取页面的URL, Title, Source

      Selenium WebDriver广泛用于web自动化,这篇文章主要是介绍浏览器的一些操作。主要以Firefox为例, 因Chrome Driver是Chromium 项目自己支持和维护的,所以需另外下载安装Chrome Driver,详细介绍查阅wiki 。注:但它的操作方式与firefox是类似。

    打开浏览器

    两种方式打开firefox。

     1 package com.annieyu.test; 
    3
    import org.openqa.selenium.firefox.FirefoxBinary; 4 import org.openqa.selenium.firefox.FirefoxDriver; 5 import org.openqa.selenium.ie.InternetExplorerDriver; 6 import org.openqa.selenium.WebDriver; 7 8 9 public class OpenBrowsers { 10 public static void main(String[] args){ 11 12 // 打开默认路径的firefox 13 WebDriver driver = new FirefoxDriver(); 14 15 // 方法1:打开指定路径下的firefox 16 System.setProperty("webdriver.firefox.bin", "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"); 17 WebDriver driver1 = new FirefoxDriver(); 18 19 // 方法2:打开指定路径下的firefox 20 File firefoxBinaryPath = new File("C:\Program Files (x86)\Mozilla Firefox\firefox.exe"); 21 FirefoxBinary firefoxBinary = new FirefoxBinary(firefoxBinaryPath); 22 WebDriver driver2 = new FirefoxDriver(firefoxBinary, null); 23 24 // 打开ie浏览器 25 WebDriver ieDriver = new InternetExplorerDriver(); 26 } 27

    从浏览器打开URL

    有两种方式,打开URL。

     1 package com.annieyu.test;
     2 import org.openqa.selenium.firefox.FirefoxDriver;
     3 import org.openqa.selenium.WebDriver;
     4 
     5 public class OpenURL {
     6     public static void main(String[] args) {
     7         // 打开默认路径的firefox
     8         WebDriver driver = new FirefoxDriver();
     9         
    10         // 定义要打开的URL路径
    11         String url = "http://www.taobao.com";
    12         
    13         // 方法1:get方法,打开URL
    14         driver.get(url);
    15 
    16         // 方法2:用navigate方法,然后调用to方法打开URL
    17         driver.navigate().to(url);
    18 
    19     }
    20 }

    关闭浏览器

    两种方式,关闭浏览器,详细见下:

     1 package com.annieyu.test;
     2 
     3 import org.openqa.selenium.WebDriver;
     4 import org.openqa.selenium.firefox.FirefoxDriver;
     5 
     6 public class CloseBrowsers {
     7     public static void main(String[] args) {
     8         WebDriver driver = new FirefoxDriver();
     9 
    10         // 方法1:关闭浏览器
    11         driver.close();
    12 
    13         // 方法2:关闭浏览器
    14         driver.quit();
    15 
    16     }
    17 
    18 }

    获取页面的Title、URL、SOURCE

     1 package com.annieyu.test;
     2 
     3 import org.openqa.selenium.WebDriver;
     4 import org.openqa.selenium.firefox.FirefoxDriver;
     5 
     6 public class GetPageResource {
     7     public static void main(String[] args) {
     8         WebDriver driver = new FirefoxDriver();
     9         
    10         String url = "http://www.taobao.com";
    11         
    12         // 打开URL
    13         driver.get(url);
    14         
    15         // 获取页面的title
    16         String title = driver.getTitle();
    17 
    18         // 获取当前页面的url
    19         String currentURL = driver.getCurrentUrl();
    20 
    21         // 获取页面的源码
    22         String sourceCode = driver.getPageSource();
    23         
    24         System.out.println(title + url);
    25     }
    26 }
  • 相关阅读:
    UNIX网络编程读书笔记:原始套接口
    UNIX网络编程读书笔记:UNIX域协议
    UNIX网络编程读书笔记:名字与地址转换
    链表的游标(cursor)实现
    基数排序
    UNIX网络编程读书笔记:基本SCTP套接口编程
    Android系列之Fragment(三)----Fragment和Activity之间的通信(含接口回调)
    git版本控制工具(二)----本地版本库的常用操作
    Android系列之Fragment(二)----Fragment的生命周期和返回栈
    Git版本控制工具(一)----git的安装及创建版本库
  • 原文地址:https://www.cnblogs.com/annieyu/p/3901638.html
Copyright © 2011-2022 走看看