Selenium常用API:

前面两篇示例代码中用到了一些selenium的API方法,例如定位元素的八种方法、访问url、等待、操作浏览器、获取title、点击、清理等等。

有关于selenium的常用API在园子中有写的非常详细的文章。先贴大佬文章地址:https://www.cnblogs.com/Ming8006/p/5727542.html#c1.5。

对于几种用的比较多的地方再记录一下:

等待:

  显式等待:等待条件成立,再继续执行

  示例代码中含有显式等待: 

// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese!");
}
});

  官方文档说明:An explicit wait is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.【一句话概括“等待条件发生,再继续执行的代码”】 

WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement"))) 

  在抛出TimeoutException之前最多等待10s,或者在10s发现元素则返回。ExpectedCondition函数返回类型为true或非null对象。

  在实例中无需实例化:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));

  

  隐式等待:通过设定的时长等待页面元素加载完成,再执行;如果超过设定时间,则继续执行。

  官方文档示例代码:

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));

  隐式等待方法关键字:implicitlyWait,直接调用即可。

  该实例等待10s,10s内元素加载完成则继续。

  

  强制等待:lang包下的Thread.sleep()方法,该方法应该也是debug用的最多的一种。

鼠标悬浮:

  在定位元素时,通常有些元素需要执行鼠标悬浮操作。

  使用action包下perform方法:  

    //鼠标悬浮
public void hover(String xpath) {
Actions action = new Actions(driver);
try {
action.moveToElement(driver.findElement(By.xpath(xpath))).perform();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

文件上传:

  文件上传可直接使用sendkeys传入需要上传文件的本地路径即可。

  切记:使用该方法,定位到的元素该标签必须是input类型、type=file属性,满足该条件即可用到该方法。  

driver.findElement(By.xpath(xpath)).sendKeys(filePath);

  filepath为本地文件路径及文件名。

截图:

  通常执行自动化测试脚本并非一帆风顺,或者发现流程上的一些错误等等,这个时候便需要增加一些截图,执行完成之后人为进行检查。

  官方文档给出了示例代码:  

import java.net.URL;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver; public class Testing { public void myTest() throws Exception {
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:4444/wd/hub"),
DesiredCapabilities.firefox()); driver.get("http://www.google.com"); // RemoteWebDriver does not implement the TakesScreenshot class
// if the driver does have the Capabilities to take a screenshot
// then Augmenter will add the TakesScreenshot methods to the instance
WebDriver augmentedDriver = new Augmenter().augment(driver);
File screenshot = ((TakesScreenshot)augmentedDriver).
getScreenshotAs(OutputType.FILE);
}
}

  可以看的出该处用到的是augment,中间的注释指的是如果驱动程序确实有能力进行截屏,然后Augmenter会将TaksCyEnScript方法添加到实例中。

调用JS:

  有时候需要利用JS来执行赋值、点击操作等。

  使用的是JavascriptExecutor,copy一下上方链接那位大佬的示例代码。 

    private static void runJSTest1(WebDriver driver) throws InterruptedException {
String js ="alert(\"hello,this is a alert!\")";
((org.openqa.selenium.JavascriptExecutor) driver).executeScript(js);
Thread.sleep(2000);
} private static void runJSTest2(WebDriver driver)
throws InterruptedException {
driver.get("https://www.baidu.com/");
String js ="arguments[0].click();";
driver.findElement(By.id("kw")).sendKeys("JAVA");
WebElement searchButton = driver.findElement(By.id("su"));
((org.openqa.selenium.JavascriptExecutor) driver).executeScript(js,searchButton);
Thread.sleep(2000);
}

浏览器滚动:

  一样使用的是JavascriptExecutor。  

JavascriptExecutor js = (JavascriptExecutor)driver;
String jscmd = "window.scrollTo(0," + height + ")";
js.executeScript(jscmd);

进入iframe子页面:

  通常一些元素会在iframe的标签下,那么便需要进入iframe再进行定位:  

WebElement frame = driver.findElement(By.xpath());
driver.switchTo().frame();

下拉框:

  针对下拉框selenium使用select。  

Select userselect = new Select(driver.findElement(By.xpath()));
userselect.selectByVisibleText();

切换浏览器窗口:

  webdriver中包含getWindowHandles方法。通过窗口标题去判断切换浏览器的窗口  

    public void switchWindow(String target) {
//创建 一个字符串便于之后存放句柄
String s = null;
//获取当前界面中的句柄
Set<String> handles = driver.getWindowHandles();
//循环尝试,找到目标浏览器的句柄
for(String t :handles) {
//遍历每一个句柄,判断窗口的标题是否含有预期字符
System.out.println(t);
if (driver.switchTo().window(t).getTitle().equals(target)) {
s = t;
}
}
//切换到目标句柄的界面中
try {
driver.switchTo().window(s);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

加载cookie:

//addcookie方法
driver.manage.addcookie()
//使用option读取用户文件,本地路径
ChromeOptions o = new ChromeOptions();
o.addArguments(“本地路径”)

后续~~~~

  

  

  

UI“三重天”之selenium--常用API和问题处理(三)的更多相关文章

  1. Selenium Web 自动化 - Selenium常用API

    Selenium Web 自动化 - Selenium常用API 2016-08-01 目录 1 对浏览器操作  1.1 用webdriver打开一个浏览器  1.2 最大化浏览器&关闭浏览器 ...

  2. Selenium常用API详解介绍

    转至元数据结尾   由 黄从建创建, 最后修改于一月 21, 2019 转至元数据起始   一.selenium元素定位 1.selenium定位方法 2.定位方法的用法 二.控制浏览器操作 1.控制 ...

  3. 详解介绍Selenium常用API的使用--Java语言(完整版)

    参考:http://www.testclass.net/selenium_java/ 一共分为二十个部分:环境安装之Java.环境安装之IntelliJ IDEA.环境安装之selenium.sele ...

  4. java selenium常用API(WebElement、iFrame、select、alert、浏览器窗口、事件、js) 一

     WebElement相关方法 1.点击操作 WebElement button = driver.findElement(By.id("login")); button.clic ...

  5. java selenium常用API汇总

    (WebElement.iFrame.select.alert.浏览器窗口.事件.js)     一 WebElement相关方法 1.点击操作 WebElement button = driver. ...

  6. Selenium2(java)selenium常用API 四

    WebElement相关方法 1.点击操作 WebElement button = driver.findElement(By.id("login")); button.click ...

  7. Selenium常用API用法示例集----下拉框、文本域及富文本框、弹窗、JS、frame、文件上传和下载

    元素识别方法.一组元素定位.鼠标操作.多窗口处理.下拉框.文本域及富文本框.弹窗.JS.frame.文件上传和下载 元素识别方法: driver.find_element_by_id() driver ...

  8. UI“三重天”之Selenium(一)

    关注一下UI自动化,记一记笔记. UI自动化的优缺点: 关于UI自动化的优缺点想来大家都有了解,优点:解放人力(并不是完全解放),用机器(涵盖工具.脚本等)代替人工完成测试工作,将测试用例转化为脚本实 ...

  9. Selenium常用API的使用java语言之19-调用JavaScript代码

    虽然WebDriver提供了操作浏览器的前进和后退方法,但对于浏览器滚动条并没有提供相应的操作方法.在这种情况下,就可以借助JavaScript来控制浏览器的滚动条.WebDriver提供了execu ...

随机推荐

  1. UOJ #185【ZJOI2016】 小星星

    题目链接:小星星 首先有个暴力很好想.令\(f_{i,j,S}\)表示把\(i\)这棵子树对应到原图中的\(S\)集合,\(i\)号点对应到了\(j\)号点的方案数.这玩意儿复杂度是\(O(3^nn^ ...

  2. ElasticSearch安装和head插件安装

    本文主要介绍elasticsearch5.0安装及head插件安装.确保系统已经安装好jdk1.8以上,操作系统CentOS6以上. 一.elasticsearch安装配置 1.官网下载源码包 下载不 ...

  3. [转] oracle 监听

    oracle 监听 启动监听:lsnrctl start 查看监听:lsnrctl status 停止监听:lsnrctl stop 1.oracle 数据服务器包括:实例进程和数据库: 实例进程包括 ...

  4. 手把手教你如何加入到github的开源世界

    我曾经一直想加入到开源项目中,但是因为没有人指导流程,网上看了很多,基本都是说了个大概,如果你也是一个初出茅庐的人,那么,我将以自己提交的一次开源代码为例,教会你步入开源的世界. 1,首先登陆到htt ...

  5. mysqli使用记录

    1.批量插入数据insert 方式一:insert into tableName (id,name,age) values ('1','张三','12'),('2','李四','16'),('3',' ...

  6. iOS多线程GCD详解

    在这之前,一直有个疑问就是:gcd的系统管理多线程的概念,如果你看到gcd管理多线程你肯定也有这样的疑问,就是:并发队列怎么回事,即是队列(先进先出)怎么会并发,本人郁闷了好久,才发现其实cgd管理多 ...

  7. JDK5的新特性:泛型、可变参数、静态导入

    数组可以在创建的时候就指定存放的数据类型,这样放入不同类型的时候就会发生编译错误. 而集合却可以存储多种不同类型,这样的话如果是遍历的时候在集合中装了好多不同的数据类型的时候,十分容易发生类型转换错误 ...

  8. openResty缓存前移(到达nginx端)

    一.openResty的理解 OpenResty是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高 ...

  9. MyEclipse web jsp 如何调试

    MyEclipse如何调试 | 浏览:882 | 更新:2014-03-13 17:38 1 2 3 4 5 分步阅读 当程序写好之后,如何调试呢? 我们在MyEclipse中jav添加断点,运行de ...

  10. Scss开发临时学习过程||webpack、npm、gulp配置

    SCSS语法: 假设变量申明带有!default,那么如果在此申明之前没有这个变量的申明,则用这个值,反之如果之前有申明,则用申明的值. ‘...’传递多个参数: @mixin box-shadow( ...