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 }
  • 相关阅读:
    2020-03-12推荐阅读链接
    一问带你区分清楚Authentication,Authorization以及Cookie、Session、Token
    Alibaba-技术专区-开源项目之Nacos源码分析
    Alibaba-技术专区-开源项目之Nacos功能特性
    Alibaba-技术专区-开源项目之Nacos环境准备
    Java-技术专区-javaAgent(插桩,attach)
    SkyWalking 6.x 源码分析 —— 调试环境搭建 (非原创)
    SkyWalking 6.2-Docker-Compose搭建单体
    Zookeeper-技术专区-配置以及学习
    Java-技术专区-设计模式-reactor模式
  • 原文地址:https://www.cnblogs.com/annieyu/p/3901638.html
Copyright © 2011-2022 走看看