selenium常用api详解介绍(代码片段)

workey workey     2023-03-09     628

关键词:


转至元数据结尾

 

转至元数据起始

 

 

 

一、selenium元素定位

1.selenium定位方法


Selenium提供了8种定位方式。

  • id
  • name
  • class name
  • tag name
  • link text
  • partial link text
  • xpath
  • css selector


这8种定位方式在Python selenium中所对应的方法为:

  • findElement(By.id())
  • findElement(By.name())
  • findElement(By.className())
  • findElement(By.tagName())
  • findElement(By.linkText())
  • findElement(By.partialLinkText())
  • findElement(By.xpath())
  • findElement(By.cssSelector())

2.定位方法的用法

假如我们有一个Web页面,通过前端工具(如,Firebug)查看到一个元素的属性是这样的。

  1. <html>

  2. <head>

  3. <body link="#0000cc">

  4. <id="result_logo" href="/" onmousedown="return c(‘fm‘:‘tab‘,‘tab‘:‘logo‘)">

  5. <form id="form" class="fm" name="f" action="/s">

  6. <span class="soutu-btn"></span>

  7. <input id="kw" class="s_ipt" name="wd" value="" maxlength="255" autocomplete="off">

我们的目的是要定位input标签的输入框。

  • 通过id定位:
    driver.findElement(By.id("kw"))
  • 通过name定位:
    driver.findElement(By.name("wd"))
  • 通过class name定位:
    driver.findElement(By.className("s_ipt"))
  • 通过tag name定位:
    driver.findElement(By.tagName("input"))
  • 通过xpath定位,xpath定位有N种写法,这里列几个常用写法:
    1. driver.findElement(By.xpath("//*[@id=‘kw‘]"))

    2. driver.findElement(By.xpath("//*[@name=‘wd‘]"))

    3. driver.findElement(By.xpath("//input[@class=‘s_ipt‘]"))

    4. driver.findElement(By.xpath("/html/body/form/span/input"))

    5. driver.findElement(By.xpath("//span[@class=‘soutu-btn‘]/input"))

    6. driver.findElement(By.xpath("//form[@id=‘form‘]/span/input"))

    7. driver.findElement(By.xpath("//input[@id=‘kw‘ and @name=‘wd‘]"))

  • 通过css定位,css定位有N种写法,这里列几个常用写法:
    1. driver.findElement(By.cssSelector("#kw")

    2. driver.findElement(By.cssSelector("[name=wd]")

    3. driver.findElement(By.cssSelector(".s_ipt")

    4. driver.findElement(By.cssSelector("html > body > form > span > input")

    5. driver.findElement(By.cssSelector("span.soutu-btn> input#kw")

    6. driver.findElement(By.cssSelector("form#form > span > input")

接下来,我们的页面上有一组文本链接。

  1. <a class="mnav" href="http://news.baidu.com" name="tj_trnews">新闻</a>

  2. <a class="mnav" href="http://www.hao123.com" name="tj_trhao123">hao123</a>

  • 通过link text定位:
    1. driver.findElement(By.linkText("新闻")

    2. driver.findElement(By.linkText("hao123")

  • 通过partialLink text定位:
    1. driver.findElement(By.partialLinkText("新")

    2. driver.findElement(By.partialLinkText("hao")

    3. driver.findElement(By.partialLinkText("123")


关于xpaht和css的定位比较复杂,请参考: xpath语法css选择器

二、控制浏览器操作

1.控制浏览器窗口大小


有时候我们希望能以某种浏览器尺寸找开,访问的页面在这种尺寸下运行。例如可以将浏览器设置成移动端大小(480* 800),然后访问移动站点,对其样式进行评估;WebDriver 提供了 manage().window().setSize()方法来设置浏览器的大小。

  • maximize() 设置浏览器最大化
  • setSize() 设置浏览器宽高
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import org.openqa.selenium.Dimension;
 
import org.openqa.selenium.WebDriver;
 
import org.openqa.selenium.chrome.ChromeDriver;
public class Browser
public static void main(String[] args) throws InterruptedException
    WebDriver driver= new ChromeDriver();
    driver.get("https://www.baidu.cn");
    driver.manage().window().maximize();
    Thread.sleep(2000);
    driver.get("https://m.baidu.cn");
 
    driver.manage().window().setSize(new Dimension(480800));
 
    Thread.sleep(2000);
    driver.quit();
    

在 PC 端执行自动化测试脚本大多的情况下是希望浏览器在全屏幕模式下执行, 那么可以使用 maximize()方法使打开的浏览器全屏显示, 其用法与 setSize()相同, 但它不需要任何参数。

2.控制浏览器后退、前进


在使用浏览器浏览网页时,浏览器提供了后退和前进按钮,可以方便地在浏览过的网页之间切换,WebDriver也提供了对应的back()和forward()方法来模拟后退和前进按钮。下面通过例子来演示这两个方法的使用。

  • back() 模拟浏览器后退按钮
  • forward() 模拟浏览器前进按钮
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import org.openqa.selenium.WebDriver;
 
import org.openqa.selenium.chrome.ChromeDriver;
 
import org.openqa.selenium.By;
public class BrowserGo
    public static void main(String[] args) throws InterruptedException
        WebDriver driver = new ChromeDriver();
        //get 到百度首页
        driver.get("https://www.baidu.com/");
        System.out.printf("now accesss %s ", driver.getCurrentUrl());
        Thread.sleep(2000);
 
        //点击“新闻” 链接
        driver.findElement(By.linkText("新闻")).click();
        System.out.printf("now accesss %s ", driver.getCurrentUrl());
        Thread.sleep(2000);
        //执行浏览器后退
        driver.navigate().back();
        System.out.printf("back to %s ", driver.getCurrentUrl());
        Thread.sleep(2000);
        //执行浏览器前面
        driver.navigate().forward();
        System.out.printf("forward to %s ", driver.getCurrentUrl());
        Thread.sleep(2000);
        driver.quit();
    

 

为了看清脚本的执行过程,下面每操作一步都通过printf()方法来打印当前的URL地址。

3.刷新页面


有时候需要手动刷新(F5) 页面。

  • refresh() 刷新页面(F5)
  1. ……

  2. //刷新页面

  3. driver.navigate().refresh();

  4. ……

三、WebDriver常用方法


前面我们已经学习了定位元素, 定位只是第一步, 定位之后需要对这个元素进行操作, 或单击(按钮) 或 输入(输入框) , 下面就来认识这些最常用的方法。 

1.WebDriver 常用方法


下面先来认识 WebDriver 中最常用的几个方法:

  • clear() 清除文本。
  • sendKeys(*value) 模拟按键输入。
  • click() 单击元素
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class BaiduDemo
    public static void main(String[] args)
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.baidu.com/");
        WebElement search_text = driver.findElement(By.id("kw"));
        WebElement search_button = driver.findElement(By.id("su"));
 
        search_text.sendKeys("Java");
        search_text.clear();
        search_text.sendKeys("Selenium");
        search_button.click();
        driver.quit();
    

 

clear()方法用于清除文本输入框中的内容。
sendKeys()方法模拟键盘向输入框里输入内容。 但是它的作用不仅于此, 我们还可以用它发送键盘按键, 甚至用它来指定上传的文件。
click()方法可以用来单击一个元素,前提是它是可以被单击的对象,它与 sendKeys()方法是Web页面操作中最常用到的两个方法。 其实click()方法不仅仅用于单击一个按钮,它还可以单击任何可以单击的文字/图片链接、复选框、单选框、下拉框等。

2.其它常用方法

 

  • submit()

submit()方法用于提交表单。 例如,在搜索框输入关键字之后的“回车” 操作, 就可以通过 submit()方法模拟.

  1. ……

  2. WebElement search_text = driver.findElement(By.id("kw"));

  3. search_text.sendKeys("Selenium");

  4. search_text.submit();

  5. ……

  • getSize() 返回元素的尺寸。
  • getText() 获取元素的文本。
  • getAttribute(name) 获得属性值。
  • isDisplayed() 设置该元素是否用户可见。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class BaiduDemo
    public static void main(String[] args)
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.baidu.com/");
        //获得百度输入框的尺寸
        WebElement size = driver.findElement(By.id("kw"));
        System.out.println(size.getSize());
 
        //返回百度页面底部备案信息
        WebElement text = driver.findElement(By.id("cp"));
        System.out.println(text.getText());
 
        //返回元素的属性值, 可以是 id、 name、 type 或元素拥有的其它任意属性
        WebElement ty = driver.findElement(By.id("kw"));
        System.out.println(ty.getAttribute("type"));
 
        //返回元素的结果是否可见, 返回结果为 True 或 False
        WebElement display = driver.findElement(By.id("kw"));
        System.out.println(display.isDisplayed());
        driver.quit();
    

 

打印结果:

  1. (500, 22)

  2. ©2017 Baidu 使用百度前必读 意见反馈 京 ICP 证 030173 号 京公网安备 11000002000001 号

  3. text

  4. true

四、模拟鼠标操作


通过前面例子了解到,可以使用click()来模拟鼠标的单击操作,现在的Web产品中提供了更丰富的鼠标交互方式, 例如鼠标右击、双击、悬停、甚至是鼠标拖动等功能。在WebDriver中,将这些关于鼠标操作的方法封装在ActionChains类提供。
Actions 类提供了鼠标操作的常用方法:

  • contextClick() 右击
  • clickAndHold() 鼠标点击并控制
  • doubleClick() 双击
  • dragAndDrop() 拖动
  • release() 释放鼠标
  • perform() 执行所有Actions中存储的行为


百度首页设置悬停下拉菜单。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
 
public class MouseDemo
    public static void main(String[] args)
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.baidu.com/");
        WebElement search_setting = driver.findElement(By.linkText("设置"));
        Actions action = new Actions(driver);
        action.clickAndHold(search_setting).perform();
        driver.quit();
    


导入提供鼠标操作的 ActionChains 类

  • Actions(driver) 调用Actions()类,将浏览器驱动driver作为参数传入。
  • clickAndHold() 方法用于模拟鼠标悬停操作, 在调用时需要指定元素定位。
  • perform() 执行所有ActionChains中存储的行为, 可以理解成是对整个操作的提交动作。

1.关于鼠标操作的其它方法

import org.openqa.selenium.interactions.Actions;
……
Actions action = new Actions(driver);
// 鼠标右键点击指定的元素
action.contextClick(driver.findElement(By.id("element"))).perform();
// 鼠标右键点击指定的元素
 
action.doubleClick(driver.findElement(By.id("element"))).perform();
 
// 鼠标拖拽动作, 将 source 元素拖放到 target 元素的位置。
WebElement source = driver.findElement(By.name("element"));
WebElement target = driver.findElement(By.name("element"));
action.dragAndDrop(source,target).perform();
 
// 释放鼠标
action.release().perform();

五、模拟键盘操作

Keys()类提供了键盘上几乎所有按键的方法。 前面了解到, sendKeys()方法可以用来模拟键盘输入, 除此之 外, 我们还可以用它来输入键盘上的按键, 甚至是组合键, 如 Ctrl+A、 Ctrl+C 等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
 
public class Keyboard
    public static void main(String[] args)throws InterruptedException
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.baidu.com");
        WebElement input = driver.findElement(By.id("kw"));
        //输入框输入内容
        input.sendKeys("seleniumm");
        Thread.sleep(2000);
 
        //删除多输入的一个 m
        input.sendKeys(Keys.BACK_SPACE);
        Thread.sleep(2000);
 
        //输入空格键+“教程”
        input.sendKeys(Keys.SPACE);
        input.sendKeys("教程");
        Thread.sleep(2000);
 
        //ctrl+a 全选输入框内容
        input.sendKeys(Keys.CONTROL,"a");
        Thread.sleep(2000);
 
        //ctrl+x 剪切输入框内容
        input.sendKeys(Keys.CONTROL,"x");
        Thread.sleep(2000);
 
        //ctrl+v 粘贴内容到输入框
        input.sendKeys(Keys.CONTROL,"v");
        Thread.sleep(2000);
 
        //通过回车键盘来代替点击操作
        input.sendKeys(Keys.ENTER);
        Thread.sleep(2000);
        driver.quit();
    

需要说明的是,上面的脚本没有什么实际意义,但向我们展示了模拟键盘各种按键与组合键的用法。

  • import org.openqa.selenium.Keys;


在使用键盘按键方法前需要先导入 keys 类。

以下为常用的键盘操作:
sendKeys(Keys.BACK_SPACE) 回格键(BackSpace)
sendKeys(Keys.SPACE) 空格键(Space)
sendKeys(Keys.TAB) 制表键(Tab)
sendKeys(Keys.ESCAPE) 回退键(Esc)
sendKeys(Keys.ENTER) 回车键(Enter)
sendKeys(Keys.CONTROL,‘a’) 全选(Ctrl+A)
sendKeys(Keys.CONTROL,‘c’) 复制(Ctrl+C)
sendKeys(Keys.CONTROL,‘x’) 剪切(Ctrl+X)
sendKeys(Keys.CONTROL,‘v’) 粘贴(Ctrl+V)
sendKeys(Keys.F1) 键盘 F1
……
sendKeys(Keys.F12) 键盘 F12

六、获取断言信息


不管是在做功能测试还是自动化测试,最后一步需要拿实际结果与预期进行比较。这个比较的称之为断言。
我们通常可以通过获取title 、URL和text等信息进行断言。text方法在前面已经讲过,它用于获取标签对之间的文本信息。

  • getTitle(): 用于获得当前页面的title。
  • getCurrentUrl() : 用户获得当前页面的URL。
  • getText() 获取页面文本信息。

下面同样以百度为例,介绍如何获取这些信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class AssertDemo
    public static void main(String[] args) throws InterruptedException
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.baidu.com");
        System.out.println("Search before================");
 
        //获取当前的 title 和 url
        System.out.printf("title of current page is %s ", driver.getTitle());
        System.out.printf("url of current page is %s ", driver.getCurrentUrl());
 
        //百度搜索
        WebElement search = driver.findElement(By.id("kw"));
        search.sendKeys("Selenium");
        search.sendKeys(Keys.ENTER);
        Thread.sleep(2000);
        System.out.println("Search after================");
 
        //获取当前的 title 和 url
        System.out.printf("title of current page is %s ", driver.getTitle());
        System.out.printf("url of current page is %s ", driver.getCurrentUrl());
 
        //获取第一条搜索结果的标题
        WebElement result = driver.findElement(By.xpath("//div[@id=‘content_left‘]/div/h3/a"));
        System.out.println(result.getText());
        driver.quit();
    

打印结果:

  1. Search before================

  2. title of current page is 百度一下, 你就知道

  3. url of current page is https://www.baidu.com/

  4.  

  5. Search after================

  6. title of current page is Selenium_百度搜索

  7. url of current page is

  8. https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=Selenium&rsv_pq=9be

  9. 4680700a485c1&rsv_t=e925U%2F%2B9SBTqmRI%2BuARg0%2BTCzrrZWn4jOBJkb1OS2vUjMrZsq5VblQ7toD8

  10. &rqlang=cn&rsv_enter=1&rsv_sug3=8&rsv_sug2=0&inputT=155&rsv_sug4=155

  11. Selenium - Web Browser Automation

七、设置元素等待

WebDriver提供了两种类型的等待:显式等待和隐式等待。

1.显示等待

WebDriver提供了显式等待方法,专门针对某个元素进行等待判断。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedCondition;
 
 
public class TimeOut01
    public static void main(String[]args) throws InterruptedException
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.baidu.com");
 
        //显式等待, 针对某个元素等待
        WebDriverWait wait = new WebDriverWait(driver,10,1);
        wait.until(new ExpectedCondition<WebElement>()
            @Override
            public WebElement apply(WebDriver text)
                return text.findElement(By.id("kw"));
            
        ).sendKeys("selenium");
 
        driver.findElement(By.id("su")).click();
        Thread.sleep(2000);
        driver.quit();
    

WebDriverWait类是由WebDirver提供的等待方法。在设置时间内,默认每隔一段时间检测一次当前页面元素是否存在,如果超过设置时间检测不到则抛出异常。具体格式如下:
WebDriverWait(driver, 10, 1)
driver: 浏览器驱动。 10: 最长超时时间, 默认以秒为单位。 1: 检测的的间隔(步长) 时间, 默认为 0.5s。

2.隐式等待

WebDriver 提供了几种方法来等待元素。

  • implicitlyWait。识别对象时的超时时间。过了这个时间如果对象还没找到的话就会抛出NoSuchElement异常。
  • setScriptTimeout。异步脚本的超时时间。WebDriver可以异步执行脚本,这个是设置异步执行脚本脚本返回结果的超时时间。
  • pageLoadTimeout。页面加载时的超时时间。因为WebDriver会等页面加载完毕再进行后面的操作,所以如果页面超过设置时间依然没有加载完成,那么WebDriver就会抛出异常。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import java.util.concurrent.TimeUnit;
 
public class TimeOut02
    public static void main(String[] args)
        WebDriver driver = new ChromeDriver();
        //页面加载超时时间设置为 5s
        driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
        driver.get("https://www.baidu.com/");
 
        //定位对象时给 10s 的时间, 如果 10s 内还定位不到则抛出异常
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.id("kw")).sendKeys("selenium");
 
        //异步脚本的超时时间设置成 3s
        driver.manage().timeouts().setScriptTimeout(3, TimeUnit.SECONDS);
        driver.quit();
    

 

八、定位一组元素

在第(五)节我们已经学习了8种定位方法, 那8种定位方法是针对单个元素定位的, WebDriver还提供了另外8种用于定位一组元素的方法。

  1. import org.openqa.selenium.By;

  2. ......

  3. findElements(By.id())

  4. findElements(By.name())

  5. findElements(By.className())

  6. findElements(By.tagName())

  7. findElements(By.linkText())

  8. findElements(By.partialLinkText())

  9. findElements(By.xpath())

  10. findElements(By.cssSelector())

定位一组元素的方法与定位单个元素的方法类似,唯一的区别是在单词 findElement 后面多了一个 s 表示复数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
 
public class ElementsDemo
    public static void main(String[] args) throws InterruptedException
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.baidu.com/");
        WebElement search_text = driver.findElement(By.id("kw"));
        search_text.sendKeys("selenium");
        search_text.submit();
        Thread.sleep(2000);
 
        //匹配第一页搜索结果的标题, 循环打印
        List<WebElement> search_result = driver.findElements(By.xpath("//div/div/h3"));
 
        //打印元素的个数
        System.out.println(search_result.size());
 
        // 循环打印搜索结果的标题
        for(WebElement result : search_result)
            System.out.println(result.getText());
        
        System.out.println("-------我是分割线---------");
 
        //打印第n结果的标题
        WebElement text = search_result.get(search_result.size() - 10);
        System.out.println(text.getText());
        driver.quit();
    

打印结果:

  1. 15

  2. selenium java 教程-90 天从入门到高薪「学习必看」

  3. python selenium 视频-90 天从入门到高薪「学习必看」

  4. Selenium - Web Browser Automation

  5. 功能自动化测试工具——Selenium 篇

  6. Selenium Documentation — Selenium Documentation

  7. selenium + python 自动化测试环境搭建 - 虫师 - 博客园

  8. selenium_百度翻译

  9. Selenium_百度百科

  10. 怎样开始用 selenium 进行自动化测试(个人总结)_百度经验

  11. Selenium 官网教程_selenium 自动化测试实践_Selenium_领测软件测试网

  12. Selenium - 开源中国社区

  13. selenium 是什么?_百度知道

  14. selenium-0 基础入学, 先就业后付款!

  15. selenium, 亚马逊官网, 正品低价, 货到付款!

  16. selenium java 教程-90 天从入门到高薪「学习必看」

  17. -------我是分割线---------

  18. selenium + python 自动化测试环境搭建 - 虫师 - 博客园

九、多表单切换

在 Web 应用中经常会遇到 frame/iframe 表单嵌套页面的应用, WebDriver 只能在一个页面上对元素识别与 定位, 对于 frame/iframe 表单内嵌页面上的元素无法直接定位。 这时就需要通过 switchTo().frame()方法将当前定 位的主体切换为 frame/iframe 表单的内嵌页面中。

  1. <html>

  2. <body>

  3. ...

  4. <iframe id="x-URS-iframe" ...>

  5. <html>

  6. <body>

  7. ...

  8. <input name="email" >

126邮箱登录框的结构大概是这样子的,想要操作登录框必须要先切换到iframe表单。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class MailLogin
    public static void main(String[] args)
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.126.com");
        WebElement xf = driver.findElement(By.xpath("//*[@id=‘loginDiv‘]/iframe"));
        driver.switchTo().frame(xf);
        driver.findElement(By.name("email")).clear();
        driver.findElement(By.name("email")).sendKeys("username");
        driver.findElement(By.name("password")).clear();
        driver.findElement(By.name("password")).sendKeys("password");
        driver.findElement(By.id("dologin")).click();
        driver.switchTo().defaultContent();
        //……
    

如果完成了在当前表单上的操作,则可以通过switchTo().defaultContent()方法跳出表单。

十、多窗口切换


在页面操作过程中有时候点击某个链接会弹出新的窗口, 这时就需要主机切换到新打开的窗口上进行操作。WebDriver提供了switchTo().window()方法可以实现在不同的窗口之间切换。
以百度首页和百度注册页为例,在两个窗口之间的切换如下图。

实现窗口切换的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class MoreWindows
    public static void main(String[] arge) throws InterruptedException
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.baidu.com");
 
        //获得当前窗口句柄
        String search_handle = driver.getWindowHandle();
 
        //打开百度注册窗口
        driver.findElement(By.linkText("登录")).click();
        Thread.sleep(3000);
        driver.findElement(By.linkText("立即注册")).click();
 
        //获得所有窗口句柄
        Set<String> handles = driver.getWindowHandles();
 
        //判断是否为注册窗口, 并操作注册窗口上的元素
        for(String handle : handles)
            if (handle.equals(search_handle)==false)
                //切换到注册页面
                driver.switchTo().window(handle);
                System.out.println("now register window!");
                Thread.sleep(2000);
                driver.findElement(By.name("userName")).clear();
                driver.findElement(By.name("userName")).sendKeys("user name");
                driver.findElement(By.name("phone")).clear();
                driver.findElement(By.name("phone")).sendKeys("phone number");
                //......
                Thread.sleep(2000);
                //关闭当前窗口
                driver.close();
            
        
        Thread.sleep(2000);
        driver.quit();
    

在本例中所涉及的新方法如下:

  • getWindowHandle(): 获得当前窗口句柄。
  • getWindowHandles(): 返回的所有窗口的句柄到当前会话。
  • switchTo().window(): 用于切换到相应的窗口,与上一节的switchTo().frame()类似,前者用于不同窗口的切换, 后者用于不同表单之间的切换。

十一、下拉框选择

有时我们会碰到下拉框,WebDriver提供了Select类来处理下接框。
如百度搜索设置的下拉框,如下图:

搜索下拉框实现代码如下:

  1. <select id="nr" name="NR">

  2. <option value="10" selected>每页显示 10 条</option>

  3. <option value="20">每页显示 20 条</option>

  4. <option value="50">每页显示 50 条</option>

  5. <select>

操作下接框代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
 
public class SelectDemo
    public static void main(String[] args) throws InterruptedException
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.baidu.com");
        driver.findElement(By.linkText("设置")).click();
        driver.findElement(By.linkText("搜索设置")).click();
        Thread.sleep(2000);
 
        //<select>标签的下拉框选择
        WebElement el = driver.findElement(By.xpath("//select"));
        Select sel = new Select(el);
        sel.selectByValue("20");
        Thread.sleep(2000);
        driver.quit();
    

 

Select类用于定位select标签。 selectByValue()方法符用于选取<option>标签的value值。

十二、警告框处理


在 WebDriver中处理JavaScript所生成的alert、confirm以及prompt十分简单,具体做法是使用switch_to_alert()方法定位到alert/confirm/prompt,然后使用text/accept/dismiss/sendKeys等方法进行操作。

  • getText(): 返回 alert/confirm/prompt 中的文字信息。
  • accept(): 接受现有警告框。
  • dismiss(): 解散现有警告框。
  • sendKeys(keysToSend): 发送文本至警告框。
  • keysToSend: 将文本发送至警告框。

如下图,百度搜索设置弹出的窗口是不能通过前端工具对其进行定位的,这个时候就可以通过switchTo().alert()方法接受这个弹窗。

接受一个警告框的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class AlertDemo
    public static void main(String[] args) throws InterruptedException
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.baidu.com");
        driver.findElement(By.linkText("设置")).click();
        driver.findElement(By.linkText("搜索设置")).click();
        Thread.sleep(2000);
 
        //保存设置
        driver.findElement(By.className("prefpanelgo")).click();
 
        //接收弹窗
        driver.switchTo().alert().accept();
        Thread.sleep(2000);
        driver.quit();
    

 

十三、文件上传

对于通过input标签实现的上传功能,可以将其看作是一个输入框,即通过sendKeys()指定本地文件路径的方式实现文件上传。
创建upfile.html文件,代码如下:

  1. <html>

  2. <head>

  3. <meta http-equiv="content-type" content="text/html;charset=utf-8" />

  4. <title>upload_file</title>

  5. <link href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" />

  6. </head>

  7. <body>

  8. <div class="row-fluid">

  9. <div class="span6 well">

  10. <h3>upload_file</h3>

  11. <input type="file" name="file" />

  12. </div>

  13. </div>

  14. </body>

  15. <script src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></scrip>

  16. </html>

通过浏览器打开upfile.html文件,功能如下图。

接下来通过sendKeys()方法来实现文件上传。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
 
 
public class UpFileDemo
    public static void main(String[] args) throws InterruptedException
        WebDriver driver = new ChromeDriver();
        File file = new File("./HTMLFile/upfile.html");
        String filePath = file.getAbsolutePath();
        driver.get(filePath);
 
        //定位上传按钮, 添加本地文件
        driver.findElement(By.name("file")).sendKeys("D:\upload_file.txt");
        Thread.sleep(5000);
        driver.quit();
    

 

十四、浏览器cookie操作

有时候我们需要验证浏览器中Cookie是否正确, 因为基于真实Cookie的测试是无法通过白盒测试和集成测试进行的。WebDriver提供了操作Cookie的相关方法可以读取、 添加和删除Cookie信息。
WebDriver 操作Cookie的方法:

  • getCookies() 获得所有 cookie 信息。
  • getCookieNamed(String name) 返回字典的key为“name”的Cookie信息。
  • addCookie(cookie dict) 添加Cookie。“cookie_dict”指字典对象,必须有 name和value值。
  • deleteCookieNamed(String name) 删除Cookie 信息。 “name”是要删除的 cookie的名称; “optionsString” 是该Cookie的选项,目前支持的选项包括“路径” , “域” 。
  • deleteAllCookies() 删除所有 cookie 信息。

下面通过 geCookies()来获取当前浏览器的 cookie 信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Set;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.Cookie;
 
public class CookieDemo
    public static void main(String[] args)
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.baidu.com");
        Cookie c1 = new Cookie("name""key-aaaaaaa");
        Cookie c2 = new Cookie("value""value-bbbbbb");
        driver.manage().addCookie(c1);
        driver.manage().addCookie(c2);
 
        //获得 cookie
        Set<Cookie> coo = driver.manage().getCookies();
        System.out.println(coo);
 
        //删除所有 cookie
        //driver.manage().deleteAllCookies();
        driver.quit();
    

 

打印结果:

[BIDUPSID=82803D3E2DAD0F5342D22C8F96B9E088; expires=星期六, 24 二月 208512:40:10 CST; path=/; domain=.baidu.com, name=key-aaaaaaa; path=/;domain=www.baidu.com, PSTM=1486301167; expires=星期六, 24 二月 2085 12:40:10 CST;path=/; domain=.baidu.com,H_PS_PSSID=1437_21094_21943_22023; path=/;domain=.baidu.com, BD_UPN=12314753; expires=星期三, 15 二月 2017 09:26:04 CST;path=/; domain=www.baidu.com, value=value-bbbbbb; path=/;domain=www.baidu.com,BAIDUID=82803D3E2DAD0F5342D22C8F96B9E088:FG=1; expires=星期六, 24 二月 208512:40:10 CST; path=/; domain=.baidu.com, BD_HOME=0; path=/;domain=www.baidu.com, __bsi=16852840641557463410_00_0_I_R_1_0303_C02F_N_I_I_0;expires=星期日, 05 二月 2017 09:26:10 CST; path=/; domain=.www.baidu.com]

十五、调用JavaScript代码

虽然WebDriver提供了操作浏览器的前进和后退方法,但对于浏览器滚动条并没有提供相应的操作方法。在这种情况下,就可以借助JavaScript来控制浏览器的滚动条。WebDriver提供了executeScript()方法来执行JavaScript代码。
用于调整浏览器滚动条位置的JavaScript代码如下:

  1. <!-- window.scrollTo(左边距,上边距); -->

  2. window.scrollTo(0,450);

window.scrollTo()方法用于设置浏览器窗口滚动条的水平和垂直位置。方法的第一个参数表示水平的左间距,第二个参数表示垂直的上边距。其代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.JavascriptExecutor;
 
public class JSDemo
    public static void main(String[] args) throws InterruptedException
        WebDriver driver = new ChromeDriver();
 
        //设置浏览器窗口大小
        driver.manage().window().setSize(new Dimension(700600));
        driver.get("https://www.baidu.com");
 
        //进行百度搜索
        driver.findElement(By.id("kw")).sendKeys("webdriver api");
        driver.findElement(By.id("su")).click();
        Thread.sleep(2000);
 
        //将页面滚动条拖到底部
        ((JavascriptExecutor)driver).executeScript("window.scrollTo(100,450);");
        Thread.sleep(3000);
        driver.quit();
    

 

通过浏览器打开百度进行搜索,并且提前通过window().setSize()方法将浏览器窗口设置为固定宽高显示,目的是让窗口出现水平和垂直滚动条。然后通过executeScript()方法执行JavaScripts代码来移动滚动条的位置。
(二十)获取窗口截图
自动化用例是由程序去执行,因此有时候打印的错误信息并不十分明确。如果在脚本执行出错的时候能对当前窗口截图保存,那么通过图片就可以非常直观地看出出错的原因。 WebDriver提供了截图函数getScreenshotAs()来截取当前窗口。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.TakesScreenshot;
 
public class GetImg
    public static void main(String[] arge)
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.baidu.com");
        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        try 
            FileUtils.copyFile(srcFile,new File("d:\screenshot.png"));
         catch (IOException e)
            e.printStackTrace();
        
        driver.quit();
    

 

脚本运行完成后打开D盘,就可以找到screenshot.png图片文件了

python+selenium——详解介绍selenium常用api的使用--python语言(完整版)

参考:http://www.51testing.com/html/03/n-3725703-2.html fromseleniumimportwebdriverdriver=webdriver.Firefox()#指定浏览器驱动#对象实例化--------------------------------------------------------------------------url1=‘https://www.baidu.com‘#需要打开的网址driver.get(url1)#... 查看详情

『心善渊』selenium3.0基础—4selenium基础元素定位详解(代码片段)

目录1、什么是元素定位2、Selenium元素定位常用API(1)By_id定位(2)by_name定位(3)by_class_name定位(4)by_tag_name定位(5)by_link_text定位(6)by_partial_link_text定位1、什么是元素定位元素定位就是查找HTML元素的过程。HTML元素指的是... 查看详情

springsecurity认证详解(代码片段)

...录页面编写登录页面修改配置类编写控制器认证过程其他常用配置失败跳转自定义请求中的用户名和密码自定义登录成功处理器自定义登录失败处理器自定义认证逻辑案例相关API介绍UserDetailsService自定 查看详情

常用的字符串函数详解(代码片段)

字符串函数strlen()介绍作用举例使用strlen()函数模拟实现strlen()函数strcpy()介绍作用举例使用strcpy()函数模拟实现strcpy()函数strcpy()函数注意事项strncpy()函数介绍举例使用strncpy()函数strcat()介绍作用举例使用strcat()函数模拟实现strcat()... 查看详情

string字符串详解/常用api(代码片段)

String详解/常用API简介  String是不可改变的字符串序列。String为字符串常量  StringBuilder与StringBuffer均为可改变的字符串序列。为字符串变量    StringBuilder是非线程安全的(JDK1.5)    StringBuffer是线程安全的(JDK1.0) ... 查看详情

分享知识-快乐自己:hibernate框架常用api详解(代码片段)

1):Configuration配置对象Configuration用于加载配置文件。1):调用configure()方法,加载src下的hibernate.cfg.xml文件    Configurationconf=newConfiguration().configure();2):如果配置文件不符合默认的加载规则,我们可以用:    newConfi... 查看详情

javahashmap源码分析——常用方法详解(代码片段)

...介绍了HashMap的基本概念,这一篇着重介绍HasHMap中的一些常用方法:put()get()**resize()**首先介绍resize()这个方法,在我看来这是HashMap中一个非常重要的方法,是用来调整HashMap中table的容量的,在很多操作中多需要重新计算容量。源... 查看详情

zephryuartdevice详解(代码片段)

 目录前言一、UartDevice介绍1常用API介绍1.1uart_configure1.2uart_get1.3 uart_tx1.4 uart_tx_abort1.5 uart_rx_enable1.6 uart_poll_in1.7 uart_poll_out1.8 uart_callback_set1.9 uart_irq_callback_set(旧版)1. 查看详情

zephryuartdevice详解(代码片段)

 目录前言一、UartDevice介绍1常用API介绍1.1uart_configure1.2uart_get1.3 uart_tx1.4 uart_tx_abort1.5 uart_rx_enable1.6 uart_poll_in1.7 uart_poll_out1.8 uart_callback_set1.9 uart_irq_callback_set(旧版)1. 查看详情

zephryuartdevice详解(代码片段)

目录前言一、UartDevice介绍1常用API介绍1.1uart_configure1.2uart_get1.3 uart_tx1.4 uart_tx_abort1.5 uart_rx_enable1.6 uart_poll_in1.7 uart_poll_out1.8 uart_callback_set1.9 uart_irq_callback_set(旧版)1.9 查看详情

python+selenium+autoit模拟键盘实现另存为上传下载操作详解(代码片段)

前言在web页面中,可以使用selenium的定位方式来识别元素,从而来实现页面中的自动化,但对于页面中弹出的文件选择框,selenium就实现不了了,所以就需引用AutoIt工具来实现。AutoIt介绍AutoIt简单介绍下,AutoIt目前最新是v3版本,... 查看详情

resttemplate|使用详解(代码片段)

...个同步的RestAPI客户端。下面我们就来介绍下RestTemplate的常用功能。RestTemplate的使用RestTemplate支持所有Restful风格方法,你可以根据需要进行选择,这里我们只介绍一些常用的方法。所有方法都支持URI模板和URI参数,支... 查看详情

selenium---常用元素等待的三种方法(代码片段)

  在写appium的时候介绍了等待时间,其实selenium这里也是一样的,分别是强制等待,隐式等待,显示等待。详情见:appium---等待时间强制等待看到名称就应该知道,强制等待,就是设置多少秒,就必须等待多少秒,才能继续往... 查看详情

poi使用详解(代码片段)

ApachePOI使用详解1.POI结构与常用类(1)POI介绍    ApachePOI是Apache软件基金会的开源项目,POI提供API给Java程序对MicrosoftOffice格式档案读和写的功能。.NET的开发人员则可以利用NPOI(POIfor.NET)来存取MicrosoftOffice文档的功能。(... 查看详情

selenium-介绍和安装(代码片段)

前戏相信大家对web自动化selenium都不陌生,是一个web自动化框架,我在第一家公司的时候,产品是两个星期一个版本,每一次发布测试都要进行回归测试,也就是大家说的点点点,后来我就想,能不能做成自动化,让代码来替我... 查看详情

《手把手教你》系列基础篇(七十二)-java+selenium自动化测试-框架设计基础-testng简单介绍(详解教程)(代码片段)

1.简介前面文章细心的小伙伴会发现宏哥在运行测试用例的时候有的是在main方法下,而有的不需要用main方法去执行用例,那么为什么有的就不需要在main方法下就能够成功运行测试用例了。这就需要单元测试框架的支持,这篇宏... 查看详情

jpa使用详解(代码片段)

...2.JPA的优势3.JPA与hibernate的关系JPA入门案例1.搭建开发环境常用注解说明JPA主键生成策略JPA的API介绍1.Persistence对象2.EntityManagerFactory3.EntityManager4.EntityTransactionORM思想1.ORM概述ORM(O 查看详情

17-docker-常用命令详解-dockercreate(代码片段)

常用命令前言dockercreate作用语法格式options说明实际例子前言本篇来学习前面没有介绍的常用docker命令dockercreate作用创建一个新的容器但不启动它语法格式dockercreate[OPTIONS]IMAGE[COMMAND][ARG...]options说明option作用–name指定容器的名字... 查看详情