Selenium API(C#)
1 Fetching a Page
driver.Url = "http://www.google.com";
2 Locating UI Elements (WebElements)
By ID
This is the most efficient and preferred way to locate an element. Common pitfalls that UI developers make is having non-unique id’s on a page or auto-generating the id, both should be avoided. A class on an html element is more appropriate than an auto-generated id.
Example of how to find an element that looks like this:
<div id="coolestWidgetEvah">...</div>
IWebElement element = driver.FindElement(By.Id("coolestWidgetEvah"));
By Class Name
“Class” in this case refers to the attribute on the DOM element. Often in practical use there are many DOM elements with the same class name, thus finding multiple elements becomes the more practical option over finding the first element.
Example of how to find an element that looks like this:
<div class="cheese"><span>Cheddar</span></div><div class="cheese"><span>Gouda</span></div>
IList<IWebElement> cheeses = driver.FindElements(By.ClassName("cheese"));
By Tag Name
The DOM Tag Name of the element.
Example of how to find an element that looks like this:
<iframe src="..."></iframe>
IWebElement frame = driver.FindElement(By.TagName("iframe"));
By Link Text
Find the link element with matching visible text.
Example of how to find an element that looks like this:
<a href="http://www.google.com/search?q=cheese">cheese</a>
IWebElement cheese = driver.FindElement(By.LinkText("cheese"));
By CSS
Like the name implies it is a locator strategy by css. Native browser support is used by default, so please refer to w3c css selectors <http://www.w3.org/TR/CSS/#selectors>for a list of generally available css selectors. If a browser does not have native support for css queries, then Sizzle is used. IE 6,7 and FF3.0 currently use Sizzle as the css query engine.
Beware that not all browsers were created equal, some css that might work in one version may not work in another.
Example of to find the cheese below:
<div id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</span
IWebElement cheese = driver.FindElement(By.CssSelector("#food span.dairy.aged"));
By XPATH
At a high level, WebDriver uses a browser’s native XPath capabilities wherever possible. On those browsers that don’t have native XPath support, we have provided our own implementation. This can lead to some unexpected behaviour unless you are aware of the differences in the various xpath engines.
| Driver | Tag and Attribute Name | Attribute Values | Native XPath Support |
|---|---|---|---|
| HtmlUnit Driver | Lower-cased | As they appear in the HTML | Yes |
| Internet Explorer Driver | Lower-cased | As they appear in the HTML | No |
| Firefox Driver | Case insensitive | As they appear in the HTML | Yes |
This is a little abstract, so for the following piece of HTML:
<input type="text" name="example" />
<INPUT type="text" name="other" />
IList<IWebElement> inputs = driver.FindElements(By.XPath("//input"));
Using JavaScript
You can execute arbitrary javascript to find an element and as long as you return a DOM Element, it will be automatically converted to a WebElement object.
Simple example on a page that has jQuery loaded:
IWebElement element = (IWebElement) ((IJavaScriptExecutor)driver).ExecuteScript("return $('.cheese')[0]");
Selenium API(C#)的更多相关文章
- Selenium2+python自动化27-查看selenium API
前言 前面都是点点滴滴的介绍selenium的一些api使用方法,那么selenium的api到底有多少呢?本篇就叫大家如何去查看selenium api,不求人,无需伸手找人要,在自己电脑就有. p ...
- Robot Framework自动化测试(三)---Selenium API
Robot Framework Selenium API 说明: 此文档只是将最常用的UI 操作列出.更多方法请查找selenium 关键字库. 一.浏览器驱动 通过不同的浏览器执行脚本. Op ...
- Selenium2+python自动化-查看selenium API
前面都是点点滴滴的介绍selenium的一些api使用方法,那么selenium的api到底有多少呢?本篇就叫大家如何去查看selenium api,不求人,无需伸手找人要,在自己电脑就有. pydo ...
- Robot Framework自动化测试三(selenium API)
Robot Framework Selenium API 说明: 此文档只是将最常用的UI 操作列出.更多方法请查找selenium2Library 关键字库. 一.浏览器驱动 通过不同的浏览器 ...
- Selenium2+python自动化27-查看selenium API【转载】
前言 前面都是点点滴滴的介绍selenium的一些api使用方法,那么selenium的api到底有多少呢?本篇就叫大家如何去查看selenium api,不求人,无需伸手找人要,在自己电脑就有. p ...
- Selenium私房菜系列3 -- Selenium API参考手册【ZZ】
大家在编写测试案例想查阅Selenium API说明时,可以查阅本文,否则请跳过! (注:这里API版本比较老,新版本的Selenium的API在这里不一定能找到.) Selenium API说明文档 ...
- Selenium API 介绍
Selenium API 介绍 我们先前学习过元素定位,大家不知道学习得怎么样了,当你学会元素定位之后就能够跟着我的脚步学习本节Selenium 经常使用的API 介绍 Seleium 为什么能模拟人 ...
- robot framework框架selenium API
RIDE面板认识 selenium API 关键字 语法 参数 备注 Open Browser url Chrome 用不同的浏览器打开url,需要下载不同的浏览器驱动,默认火狐 Close Brow ...
- <自动化测试>之<selenium API 用法2>
不知道之前的selenium API 用法1,有没有去练习, 个人认为线性代码还是要靠敲的, 后面的模块化除了多敲还需要一定的编程思想去理解, 今天下午不是很忙就给来这儿补充点selenium api ...
- robotframework - selenium Api介绍
一.介绍下selenium常用的api *** Settings ***Library SeleniumLibraryResource baidu业务.txtResource UI分层.txt *** ...
随机推荐
- word每次打开都要选择文档类型
每次打开word07 都出现下面一个框框,说要转换文件. 在Word2013文档中,为了能更好地使用“从任意文件还原文本”功能,用户需要启用“打开时确认文件格式转换”功能,以在打开并恢复文件时出现文件 ...
- asp.net <%%> <%#%><%=%><%@%><%$%>用法与区别
1.<% %>用来绑定后台代码 如: < % for(int i=0;i<100;i++) { Reaponse.Write(i.ToString()); } %> 2. ...
- angularjs小知识
字符串和对象的转化 :angular.fromJson(jsonStr) 对象转字符串 :angular.toJson(obj) jsonStr:json字符串 obj:对象
- mysql的账户失效,之前的密码无法登录
引用自:http://blog.sina.com.cn/s/blog_682c287b0100ofz8.html 此为linux服务器下的做法 方法一: 1.关闭mysql # service ...
- 深入理解shared pool共享池之library cache系列二
背景 继续上文:深入理解shared pool共享池之library cache系列一,学习library cache数据结构,本文主要学习library cache object(lco)的数据结构 ...
- Oracle OEM建库实例
OEM是一个图形化的数据库管理员工具.它为数据库管理员提供了一个集中的系统管理工具,同时 它也是一个用来管理.诊断和调试多个数据库的工具,一个用来管理来自多个地点的多个网络节点和服务的工具.该工具可以 ...
- java中 引用类型 和 基本类型 有何区别?
栈与堆都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆. Java的堆是一个运行时数据区,类的(对象从中分配空间.这些对象通过new.newa ...
- Jquery插件之信息弹出框showInfoDialog(成功、错误、警告、通知)
一.信息种类说明: 1.1.操作成功信息 1.2.错误信息 1.3.警告信息 1.4.通知信息 二.使用说明 <!DOCTYPE html PUBLIC "-//W3C//DTD HT ...
- Global & Local Variable in Python
Following code explain how 'global' works in the distinction of global variable and local variable. ...
- django框架的网站发布后设置是否允许被别人iframe引用
例如: <iframe src="http://127.0.0.1:8008" style="width:100%;height:400px;">& ...