自动化测试框架 Selenium(3)

目录

1.前言

2.等待方式

2.1死等

2.2智能等待

3.游览器操作

3.1游览器最大化

[3.2设置游览器的宽 高](#3.2设置游览器的宽 高)

[3.3 游览器的前进和后退](#3.3 游览器的前进和后退)

3.4游览器滚动条


1.前言

本篇博客,我们将继续Selenium自动化测试的学习.在前面的章节中,俺介绍了Selenium是怎么回事,和键盘鼠标操作.还有url和title的获取,接下来我们将介绍更多Selenium的操作.

2.等待方式

Selenium中,我们有两种等待方式,分别是智能等待和死等.接下来,俺来带大家应用一下.

2.1死等

这个就很简单,是Thread类中的提供的方法. sleep(等待的时间:单位 ms).

我们用一个百度上新闻的例子来演示一下.

java 复制代码
 private static void test8() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //死等
        sleep(3000);
        //等待三秒以后点击新闻按钮
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
    }

可以观察到,在三秒后,会跳转到新闻界面.

2.2智能等待

该方式是,如果执行到一个元素定位时,就不等了.加载不出来的话.就按照设置的时间等下去.

java 复制代码
private static void test9() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //智能等待  等三分钟
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
        //等待三秒以后点击新闻按钮
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
    }

3.游览器操作

3.1游览器最大化

我们默认的打开游览器并不是最大的.如果有这个需求的话,可以通过代码处理.

java 复制代码
 private static void test10(){
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.manage().window().maximize();
    }

3.2设置游览器的宽 高

java 复制代码
 private static void test11(){
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.manage().window().setSize(new Dimension(300,400));
    }

我们把游览器的宽设置为300px,高设置为400px.

3.3 游览器的前进和后退

前进: webDriver.navigate().forward();

后退: webDriver.navigate().back();

java 复制代码
 private static void test12() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("Selenium");
        webDriver.findElement(By.cssSelector("#su")).click();
        //后退
        sleep(2000);
        webDriver.navigate().back();
        //前进
        sleep(2000);
       webDriver.navigate().forward();
    }

3.4游览器滚动条

如果我们想要游览器滚动的话,可以使用前端js代码来实现:我们把游览器滚动条滑到最底端:

document.documentElement.scrollTop=10000

java 复制代码
 private static void test13() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("Selenium");
        webDriver.findElement(By.cssSelector("#su")).click();
       sleep(3000);
        String js ="document.documentElement.scrollTop=10000";
        ((JavascriptExecutor)webDriver).executeScript(js);
    }
相关推荐
databook13 小时前
Manim实现闪光轨迹特效
后端·python·动效
Juchecar14 小时前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
用户83562907805114 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_14 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
数据智能老司机21 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机1 天前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i1 天前
drf初步梳理
python·django
每日AI新事件1 天前
python的异步函数
python