Selenium-webdriver基本使用

准备

  ① node.js 的安装和配置略

  ② Selenium-webdriver

    npm install -save selenium-webdriver

  ③ 驱动
    chromedriver  欢迎大家FQ下载:https://sites.google.com/a/chromium.org/chromedriver/downloads。然后找个环境路径存一下就可以调用了。

    IEdriver      github下载:https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver 。 同样环境路径保存。

            注意:IE玩家把internet选项-安全- 四个选项的启动保护模式都关掉!

         PhantomJS     直接解压,把bin目录放在环境路径中

    Opera以及geckodriver试了一下,对最新版opera和firefox支持并不好。可以下载较低的版本使用。

一、基本使用

const {Builder, By, Key, until, Button} = require("selenium-webdriver");
let broswer = new Builder().forBrowser('ie').build() #这里使用了ie引擎
broswer.get('http://www.baidu.com')
broswer.quit() // 表示关闭浏览器
//drive.close()表示关闭当前窗口

二、选择器

broswer.findElement(By.name('btnG'));
broswer.findElement({id:"btnG"});
element.findElement(); //同样可以对元素使用findElement方法
findElements //查找多个元素 By.className(classname)
By.css(selector) #css-selector
By.id(id)
By.name(name)
By.linkText(text)
By.partialLink(text)
By.xpath()
By.js() //Locates an elements by evaluating a JavaScript expression. The result of this expression must be an element or list of elements.

三、属性获取

//获取代码:
browser.getPageSource().then(function(souce) {console.log(souce);
//获取网页标题:
browser.getTitle().then(b=>{console.log(b)});
//获取当前url:
browser.getCurrentUrl().then(b=>{console.log(b)}); //element为web元素对象,为findelement()的返回对象
element.getText().then(b=>{console.log("text",b)}) //返回里面没有被隐藏的文字(不带标签) element.getTagName().then(b=>{console.log("Tagname",b)})
//返回标签名 element.getId().then(b=>{console.log("ID",b)})
//返回这个element服务器分配的不透明id elements.getCssValue().then(=>{console.log("CSSvalue",b)})
//返回该element的CSS属性 //其他属性:
element.getAttribute("class").then(b=>{console.log(b)})

四、等待

 
//等待元素:
browser.wait(until.elementLocated(By.id('foo')), 10000); browser.wait(function() {
return driver.getTitle().then(function(title) { console.log(11111111);
return title === 'webdriver - Google Search';
});
}, 1000); browser.wait(until.titleIs('webdriver - Google Search'), 1000) //settimeouts
browser.manage().setTimeouts(args)
// args参数
{implicit: (number|null|undefined), pageLoad: (number|null|undefined), script: (number|null|undefined)}
implicit:等待元素加载的最大时间;pageLoad等待页面加载完成的最大时间

五、操作

①input操作

//清空
element.clear();
//输入
element.sendKeys("webdriver");
element.sendKeys(Key.ENTER);
element.submit(); //以submit命令提交执行

②截图

broswer.takeScreenshot().then()  //返回页面png截图
element.takeScreenshoot().then() //返回元素png截图

③鼠标操作

//单击鼠标
element.click()
//连锁动作(action对象)
const actions = driver.actions();
actions
.keyDown(SHIFT)
.move({origin: el})
.press()
.release()
.keyUp(SHIFT)
.perform();
//actions对象以perform()作为动作链结尾,表示命令执行 //具体方法如下:
actions.clear() //清空所有动作,按键和状态
actions.click(element) //对element左键单击一次
actions.contextClick(element) //对element右键单击一次
actions.doubleClick(element) //对element双击一次
actions.dragAndDrop(ele_from,to) //单击鼠标拖动ele_from元素,如果to是坐标{x:number,y:number}移动距离;如果to是元素移动到to元素中心,并释放鼠标。 actions.keyDown(key) //按下键盘的key键
actions.keyUp(key) //释放key键 actions.move(options) //移动参数如下:
//options ({duration: (number|undefined), origin: (Origin|WebElement|undefined), x: (number|undefined), y: (number|undefined)}|undefined)
  //origin是起始位置,默认为鼠标当前位置,可以设置元素为起始位置。x,y为偏移量。duration为持续时间默认(100ms) actions.press(button) //按下鼠标 button默认是鼠标左键,有LEFT,RIGHT,MIDDLE三个值,通过Button.LEFT....获得
actions.release(button) //释放鼠标,默认左键
actions.sendKeys() //同sendkeys
actions.pause(ms,devices) //暂停ms时间,如果devices没有指定,会创建一个针对所有的事件

举例:

let actions = driver.actions({async: true});
actions.keyDown(Key.SHIFT);
actions.pause(actions.mouse()) // Pause for shift down
.press(Button.LEFT)
.move({x: 10, y: 10})
.release(Button.LEFT);
actions
.pause(
actions.keyboard(), // Pause for press left
actions.keyboard(), // Pause for move
actions.keyboard()) // Pause for release left
.keyUp(Key.SHIFT);
await actions.perform(); actions.mouse(); actions.keyboard() //分别代表鼠标和键盘设备

e.g.

更多细节参考:http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/input_exports_Actions.html

六、options对象

通过let options = broswer.manage() 获得

options.addCookie({name: 'foo', value: 'bar'})
options.deleteAllCookies() //删除所有cookies
options.deleteCookie(name) //按照name删除
options.getCookie(name) //拿到name字段的cookie值,为promise对象
options.getCookies() //返回所有cookies,为promise对象

七、nav对象

通过let nav = broswer.navigate()获得

nav有四个方法分别为:

  nav.back();

  nav.forward();

  nav.refresh();

  nav.to(url);

分别为后退,前进,刷新,跳转到url

八、其他

  ①browser.excuteScript(script) //在当前frame中执行js代码

  ②brower.switchTo() //targetlocator

    targetlocator对象参见:http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/webdriver_exports_TargetLocator.html

    其中targetlocator.frame(id)可以用来切换frame。通过parentFrame()切回

更多API详见:http://seleniumhq.github.io/selenium/docs/api/javascript/index.html

p.s. 根据浏览器版本和引擎不同,部分方法存在问题。

注意:一些执行操作的promise对象,需要合理利用async和await方法

Node selenium-webdriver的更多相关文章

  1. Selenium WebDriver 之 PageObjects 模式 by Example

    目录 1. 项目配置 2. 一个WebDriver简单例子 3. 使用Page Objects模式 4. 总结 5. Troubleshooting 6. 参考文档 本篇文章通过例子来阐述一下Sele ...

  2. Selenium WebDriver + Grid2 + RSpec之旅(四) ----通过RSpec编写测试用例

    Selenium WebDriver + Grid2 + RSpec之旅(四) ----通过RSpec编写测试用例 自动化测试和手工测试一样,是要有测试用例,有检查点和测试结果的,接下来我们通过RSp ...

  3. Selenium WebDriver + Grid2 + RSpec之旅(六) ----多浏览器的并行执行

    Selenium WebDriver + Grid2 + RSpec之旅(六) ----多浏览器的并行执行 由于浏览器的发展,浏览器种类繁多.为了保证系统能在各种浏览器上叱咤风云,减少测试人员的测试工 ...

  4. Selenium WebDriver + Grid2 + RSpec之旅(三) ----入门小例子

    Selenium WebDriver + Grid2 + RSpec之旅(三) ----入门小例子 第一个例子都是比较简单的博客园登录界面,就像学习编程语言时候都是从Hello,World!开始. 1 ...

  5. Selenium WebDriver + Grid2 + RSpec之旅(二)----Grid2的配置

    Selenium WebDriver + Grid2 + RSpec之旅(二) ----Grid2的配置 为什么要使用Selenium-Grid 分布式运行大规模的TestCase 能够通过一个中央节 ...

  6. Selenium & Webdriver 远程测试和多线程并发测试

    Selenium & Webdriver 远程测试和多线程并发测试 Selenium Webdriver自动化测试,初学者可以使用selenium ide录制脚本,然后生成java程序导入ec ...

  7. Selenium WebDriver Code

    Selenium WebDriver 用于模拟浏览器的功能,可以做网站测试用,也可以用来做crawler.我是用eclipse开发的,导入selenium-server-standalone-***. ...

  8. 使用httpclient 调用selenium webdriver

    结合上次研究的selenium webdriver potocol ,自己写http request调用remote driver代替selenium API selenium web driver ...

  9. selenium webdriver 右键另存为下载文件(结合robot and autoIt)

    首先感谢Lakshay Sharma 大神的指导 最近一直在研究selenium webdriver右键菜单,发现selenium webdriver 无法操作浏览器右键菜单,如图 如果我想右键另存为 ...

  10. Selenium Webdriver java 积累一

    Selenium Webdriver 学习: http://jarvi.iteye.com/category/203994 https://github.com/easonhan007/webdriv ...

随机推荐

  1. 【Spring】Spring3+Spring3MVCの環境構築(中)

    参考URL: https://www.cnblogs.com/lnsylt/p/10258457.html ■目録 ■環境設定 ①pom.xml <?xml version="1.0& ...

  2. 连接HTTP服务器

    一.前提 Android 系统上面默认所有Http的请求都被阻止了. 需要在androidmanifest.xml的 application标签上加入 android:usesCleartextTra ...

  3. 20175126Apollo 20175126《Java程序设计》结队编程项目——四则运算

    结队编程项目——四则运算 一.项目需求 自动生成小学四则运算题目(加.减.乘.除)统计正确率 支持整数 支持多运算符(比如生成包含100个运算符的题目) 支持真分数 需求分析: 生成四则运算:需要使用 ...

  4. hdu1251+字典树常用模板

    这里只简单给出几个常用的字典树的模板,要看具体介绍的请看:传送门 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现) ...

  5. java_29打印流

    1打印流 PrintStream 和PrintWriter 不负责数据源  只负责数据目的 2.方法 public class Demo {    public static void main(St ...

  6. php核心技术与最佳实践(笔记一)

    1.1面向对象的型与本 类是对象的抽象组织,对象是类的具体存在. 1.1.1对象的形 <?php class Person{ public $name; public $gender; publ ...

  7. CentOS 7 minimal网络配置

    centos最小化安装没有ifconfig命令,可以使用  ip addr  查看网络信息,习惯ifconfig的用户,则使用 yum -y install net-tools 即可 ; 如果yum不 ...

  8. 《笨方法学Python》加分题32

    注意一下 range 的用法.查一下 range 函数并理解它在第 22 行(我的答案),你可以直接将 elements 赋值为 range(0, 6) ,而无需使用 for 循环?在 python ...

  9. 算法 BF算法

    BF算法是字符匹配的一种算法,也称暴力匹配算法 算法思想: 从主串s1的pos位置出发,与子串s2第一位进行匹配 若相等,接着匹配后一位字符 若不相等,则返回到s1前一次匹配位置的后一位,接着与s2的 ...

  10. Python之路(第三十七篇)并发编程:进程、multiprocess模块、创建进程方式、join()、守护进程

    一.在python程序中的进程操作 之前已经了解了很多进程相关的理论知识,了解进程是什么应该不再困难了,运行中的程序就是一个进程.所有的进程都是通过它的父进程来创建的.因此,运行起来的python程序 ...