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 *** ...
随机推荐
- 前后端分离--构建前端Mock Server--windows部署rap
mock:模拟的,虚假的 mock server:模拟服务,模拟请求,模拟虚假数据 为了前后端更好的分工,接口文档是必须的,前后端都根据接口文档写代码,然后对接接口就行了. 但是,后端跟不上前端节奏, ...
- 在ASP.NET 中调用RSACryptoServiceProvider失败,提示未找到文件
在本地开发环境调试下,调试这个RSA加密是没问题的,但是部署到IIS就会报错. 原来是,在本地vs调试与IIS上运行是存在权限差异的.本地调试权限最大,IIS 次之. 所以在我们声明CspParame ...
- Java学习----有风险的代码(异常)
Exception继承了Throwable,但是它本身是有异常类的父类. RuntimeException:运行时异常 Exception->RuntimeException->NullP ...
- sql 命令操作用法
---恢复内容开始--- 远程登录数据库: mysql -u root -p 要求输入密码 ============== 查看数据库: show databases;============= 选择数 ...
- CSS_Bootstrap
①BS学习的基础 第一个例子 <!DOCTYPE html> <html lang="en"> <head> <title>Boot ...
- "System.Web" 中不存在类型或命名空间
System.Web”中不存在类型或命名空间名称script /找不到System.Web.Extensions.dll引用 添加引用就行了...“添加引用→.Net→System.Web.Ente ...
- 配置nginx支持thinkphp框架
因为nginx本身没有支持pathinfo,所以无法使用thinkphp框架,不过我们可以在配置里进行修改使其能够正常使用thinkphp. 1.修改配置支持pathinfo vi /etc/ngin ...
- Model Thinking1
Why Model Reason # 1: Intelligent Citizen of the World Reason # 2: Clearer Thinker Reason # 3: Under ...
- 机务UI设计小节
1.CSS样式 .header { background-color:#7A8692; color:White; height:30px; font-size:16px; width:100%; li ...
- 10071 - Back to High School Physics
Problem B Back to High School Physics Input: standard input Output: standard output A particle has i ...