[Selenium With C#基础教程] Lesson-02 Web元素定位
作者:Surpassme
来源:http://www.jianshu.com/p/cfd4ed1daabd
声明:本文为原创文章,如需转载请在文章页面明显位置给出原文链接,谢谢。
使用Selenium来做自动化测试,一般的流程是:查找定位元素--->操作元素--->断言,那么第一步我们需要能够完成查找并定位元素,Selenium目前提供了8种基本定位方法,可根据实际情况进行选择,如下示:
|
定位方法 |
示例 |
|
ID |
FindElement(By.Id("user")) |
|
Name |
FindElement(By.Name("username")) |
|
LinkText |
FindElement(By.LinkText("Login")) |
|
PartialLinkText |
FindElement(By.PartialLinkText("Next")) |
|
XPath |
FindElement(By.Xpath("//div[@id="login"]/input")) |
|
TagName |
FindElement(By.TagName("body")) |
|
ClassName |
FindElement(By.ClassName("table")) |
|
CSS |
FindElement(By.CssSelector("#login > input[type="text"]")) |
Step-1:启动浏览器
Selenium支持的浏览器较多,我拿目前三大使用较多的浏览器(IE、Chrome、Firefox)做一个示例,来演示如何启动三大浏览器,代码如下:
using System;
using System.Collections.ObjectModel;
//MS自带的单元测试框架
using Microsoft.VisualStudio.TestTools.UnitTesting;
//Webdriver引用
using OpenQA.Selenium;
//IE引用
using OpenQA.Selenium.IE;
//Chrome引用
using OpenQA.Selenium.Chrome;
//Firefox引用
using OpenQA.Selenium.Firefox;
namespace SeleniumDemo
{
[TestClass]
public class Lesson02
{
//获取浏览器驱动程序路径
string driverPath = AppDomain.CurrentDomain.BaseDirectory;
//定义浏览器类型枚举
public enum DriverType { IE, Chrome, Firefox };
//定义使用浏览器类型
DriverType dt = DriverType.Chrome;
IWebDriver driver;
//获取测试需要使用哪一种浏览器驱动
public IWebDriver GetDirverType(DriverType driverType)
{
switch (driverType)
{
case DriverType.IE:
driver = new InternetExplorerDriver(driverPath);
break;
case DriverType.Chrome:
driver = new ChromeDriver();
break;
case DriverType.Firefox:
//3.0之后的Selenium Webdriver才需要这样,2.0版本的可以直接以这种形式:driver = new FirefoxDriver();
FirefoxOptions fo = new FirefoxOptions();
//Firefox安装路径
fo.BrowserExecutableLocation = @"C:\Program Files\Mozilla Firefox\firefox.exe";
FirefoxDriverService fds = FirefoxDriverService.CreateDefaultService(driverPath);
driver = new FirefoxDriver(fds, fo, TimeSpan.FromSeconds());
break;
default:
break;
}
return driver;
}
[TestMethod]
public void Demo02()
{
driver = GetDirverType(dt);
//访问百度
driver.Navigate().GoToUrl("https://www.baidu.com");
//查找搜索输入框,输入Selenium
IWebElement searchText = driver.FindElement(By.Id("kw"));
//在输入前清空内容
searchText.Clear();
searchText.SendKeys("Selenium");
//查找元素
IWebElement searchBtn = driver.FindElement(By.Id("su"));
//点击搜索按钮
searchBtn.Click();
//退出浏览器
driver.Quit();
//driver.Close();
}
}
}
Close()与Quit()区别:
driver.Close():在完成操作后,仅关闭浏览器窗口,不会关闭Webdriver会话(我们在测试每个用例时,都会启动一个浏览器驱动进程,如果使用Close()方法,则浏览器驱动进程不会关闭,则使用Quit()则会自动关闭)
driver.Quit():在完成操作后,同时关闭浏览器窗口和Weddriver会话
Step-2:使用8种基本定位方法
在介绍基本的8种定位方法前,我们可以看看使用各浏览器自带的Web开发工具或插件(Firebug/Firepath等)来查看如何进行元素查看和定位:
使用IE自带的Web开发工具(可使用F12快捷键进行操作)如下所示:

在Firefox中使用Firebug+Firepath进行定位元素,如下所示:

使用Chrome自带Web开发工具进行定位元素,如下所示:

在使用Selenium Webdriver进行元素定位时,通常使用FindElement或FindElements方法结合By类返回的元素句柄来定位元素。其中By类的常用定位方式共八种,现分别介绍如下:
- By.Id()
通过元素的id属性来定位元素,具有很强的唯一性,速度也较快。
Button: driver.FindElement(By.Id("submit_btn")).Click();
Link: driver.FindElement(By.Id("cancel_link")).Click();
Text: driver.FindElement(By.Id("username")).SendKeys("admin ") ;
HTML Div元素: driver.FindElement(By.Id("alert_div")).getText();
- By.Name()
通过元素的name属性来查找元素,在当前页面中可以不唯一,可能会存在同名的情况。
driver.FindElement(By.Name("comment")).SendKeys("comment ") ;
- By.LinkText()
通过文本链接来查找元素,该链接必须能在页面中为可见状态。在一些页面会出现在出现某个操作后,链接才能显示出来,如果没有上一步操作,是无法直接使用该方法进行定位元素的。
driver.FindElement(By.LinkText("新闻")).Click();
- By. PartialLinkText ()
PartialLinkText是对Link定位的一种补充,在文本链接比较长或文本链接动态生成的时候,可以使用该方法截取一部分文本进行定准。
driver.FindElement(By.PartialLinkText("新")).Click();;
- By. XPath ()
XPath 是一门在 XML 文档中查找信息的语言,可在 XML 文档中对元素和属性进行遍历。更多XPath信息可参考:http://www.w3school.com.cn/xpath/index.asp
driver.FindElement(By.XPath("//input[@id='kw']"));
- By.TagName()
该方法可通过元素的标签名称来定位查找元素,通常该方法能定位到的元素会存在一个或多个,建议结合FindElements()方法使用。
driver.FindElements(By.TagName("button"));
- By. ClassName ()
ClassName方法是通过元素的CSS样式表所引用的伪类名称来定位查找元素。通过情况下,一个元素会存在多个CSS样式值,如下所示:
<a href="https://www.baidu.com" class="btn btn-default">百度一下</a> <input type="submit" value="提交" class="btn btn-default btn-primary"/>
我们可以以下任何一种方法进行定位元素:
driver.FindElement(By.ClassName("btn-primary ")).Click(); //提交按钮
driver.FindElement(By.ClassName("btn ")).Click(); //百度一下链接
- By.CssSelector()
在CSS中,选择器是一种模式,用于选择需要添加样式的元素,更多资料可参考:http://www.w3school.com.cn/cssref/css_selectors.asp
driver.FindElement(By.CssSelector("#kw"));
- 多元素定位
通过FindElements可以一次返回多个匹配元素,使用方法与FindElement很像。示例如下:
ReadOnlyCollection<IWebElement> elements = driver.FindElements(By.ClassName("mnav"));
本文中需要使用到的浏览器驱动及插件可到百度网盘进行下载: https://pan.baidu.com/s/1kV6uzzx 密码:9m1q
本文完整代码如下:
using System;
using System.Collections.ObjectModel;
//MS自带的单元测试框架
using Microsoft.VisualStudio.TestTools.UnitTesting;
//Webdriver引用
using OpenQA.Selenium;
//IE引用
using OpenQA.Selenium.IE;
//Chrome引用
using OpenQA.Selenium.Chrome;
//Firefox引用
using OpenQA.Selenium.Firefox; namespace SeleniumDemo
{
[TestClass]
public class Lesson02
{
//获取浏览器驱动程序路径
string driverPath = AppDomain.CurrentDomain.BaseDirectory;
//定义浏览器类型枚举
public enum DriverType { IE, Chrome, Firefox };
//定义使用浏览器类型
DriverType dt = DriverType.Chrome;
IWebDriver driver;
//获取测试需要使用哪一种浏览器驱动
public IWebDriver GetDirverType(DriverType driverType)
{
switch (driverType)
{
case DriverType.IE:
driver = new InternetExplorerDriver(driverPath);
break;
case DriverType.Chrome:
driver = new ChromeDriver();
break;
case DriverType.Firefox:
//3.0之后的Selenium Webdriver才需要这样,2.0版本的可以直接以这种形式:driver = new FirefoxDriver();
FirefoxOptions fo = new FirefoxOptions();
//Firefox安装路径
fo.BrowserExecutableLocation = @"C:\Program Files\Mozilla Firefox\firefox.exe";
FirefoxDriverService fds = FirefoxDriverService.CreateDefaultService(driverPath);
driver = new FirefoxDriver(fds, fo, TimeSpan.FromSeconds());
break;
default:
break;
}
return driver;
} [TestMethod]
public void Demo02()
{
driver = GetDirverType(dt);
//访问百度
driver.Navigate().GoToUrl("https://www.baidu.com");
//查找搜索输入框,输入Selenium
IWebElement searchText = driver.FindElement(By.Id("kw"));
60 //在输入前清空内容
searchText.Clear();
searchText.SendKeys("Selenium");
//查找元素
IWebElement searchBtn = driver.FindElement(By.Id("su"));
//点击搜索按钮
searchBtn.Click();
//退出浏览器
driver.Quit();
//driver.Close();
} [TestMethod]
//一次定位多个元素
public void FindElementsDemo()
{
driver = GetDirverType(dt);
driver.Navigate().GoToUrl("https://www.baidu.com");
ReadOnlyCollection<IWebElement> elements = driver.FindElements(By.ClassName("mnav"));
Console.WriteLine("查找到的元素个数为:{0}", elements.Count);
for (int i = ; i < elements.Count; i++)
{
Console.WriteLine(elements[i].Text);
}
driver.Quit();
}
}
}
[Selenium With C#基础教程] Lesson-02 Web元素定位的更多相关文章
- Selenium Web元素定位方法
Selenium是用于Web应用测试的自动化测试框架,可以实现跨浏览器和跨平台的Web自动化测试.Selenium通过使用WebDriver API来控制web浏览器,每个浏览器都都有一个特定的Web ...
- Selenium2学习-002-Selenium2 Web 元素定位及 XPath 编写演示示例
此文主要对 Selenium2 的 Web 元素定位及 XPath 编写示例,敬请各位亲们参阅,共同探讨.若有不足之处,敬请各位大神指正,不胜感激! 通过 Firefox(火狐)浏览器的插件 Fire ...
- [Selenium With C#基础教程] Lesson-03 超级链接
作者:Surpassme 来源:http://www.jianshu.com/p/83809943e751 声明:本文为原创文章,如需转载请在文章页面明显位置给出原文链接,谢谢. 超级链接或链接是We ...
- [Selenium With C#基础教程] Lesson-05 文本框
作者:Surpassme 来源:http://www.jianshu.com/p/7dca7d0d1ea3 声明:本文为原创文章,如需转载请在文章页面明显位置给出原文链接,谢谢. 文本框在Web页面中 ...
- [Selenium With C#学习笔记] Lesson-02 Web元素定位
使用Selenium来做自动化测试,一般的流程是:查找定位元素--->操作元素--->断言,那么第一步我们需要能够完成查找并定位元素,Selenium目前提供了8种基本定位方法,可根据实际 ...
- Robot FrameWork基础学习(四) 元素定位
元素定位 对于web自动化测试来说,就是操作页面的各种元素,在操作元素之间需要先找到元素,换句话说就是定位元素. Selenium2Library提供了非常丰富的定位器: 虽然提供了这么多种定位方式, ...
- web元素定位和appium-app元素定位
一.web页面元素定位工具介绍 1.打开google浏览器,按F12进入开发者模式,如下图: 2.用鼠标点击下图红色框中的箭头--然后鼠标移动到web页面的元素上(此处为百度框),会自动定位到对应的h ...
- [Selenium With C#基础教程] Lesson-07 复选框
作者:Surpassme 来源:http://www.jianshu.com/p/98ede43da3c3 声明:本文为原创文章,如需转载请在文章页面明显位置给出原文链接,谢谢. [作者:Surp ...
- [Selenium With C#基础教程] Lesson-04 按钮
作者:Surpassme 来源:http://www.jianshu.com/p/83d7416c4b7d 声明:本文为原创文章,如需转载请在文章页面明显位置给出原文链接,谢谢. Button通常有两 ...
随机推荐
- 【linux】linux DD命令
Linux-dd命令详解 dd 是 Linux/UNIX 下的一个非常有用的命令,作用是用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 例1:要把一张软盘的内容拷贝到另一张软盘上,利用/t ...
- SCSI协议
SCSI是一套完整的数据传输协议,其主要功能是在主机和存储设备之间传送命令.状态和块数据.在各类存储技术中,SCSI技术可谓是最重要的脊梁. SCSI协议位于操作系统和外部资源之间,它具有一系列的功能 ...
- 关于oracle数据库启动报ORA-01122,ORA-01110,ORA-01203错误的解决方法
ORACLE 数据库空间裸设备出问题了,启动oracle失败,解决方法问题现象: 启动ORACLE的时候报如下的错误: Database mounted. ORA-01 ...
- class文件格式说明
java代码编译成class文件之后,class文件里面的语法是什么样的,他的数据类型是什么以及如何存放的?? class也是一种语言写的,只不过和我们的java语法不同而已. class文件就是把j ...
- JMS消息服务模型
JMS--仅仅是一种规范,一种接口规约,一种编程模型.类似的JPA,JSR等 场景: 1.多个系统之间交互,实现可以采取RPC,但是交互复杂,基本就是点对点的方式 2.其实交互就是消息,而JMS就是消 ...
- Android开发入门——Button绑定监听事件三种方式
import android.app.Activity; import android.os.Bundle;import android.view.View;import android.widget ...
- Thymeleaf系列五 迭代,if,switch语法
1. 概述 这里介绍thymeleaf的编程语法,本节主要包括如下内容 迭代语法:th:each; iteration status 条件语法:th:if; th:unless switch语法: ...
- string ids=aduuids.Aggregate("", (m, n) => m + n+",").TrimEnd(',');
string ids=aduuids.Aggregate("", (m, n) => m + n+",").TrimEnd(',');
- SecureCRT 8.1使用经验总结
1.反空闲设置: 2.文件上传下载 上传 sudo rz -y 文本文件勾选Upload files as ASCII,图片或其他飞文本文件,去掉勾选.采用默认binary 3.文件下载 sudo s ...
- 2个版本并存的python使用新的版本安装django的方法
2个版本并存的python使用新的版本安装django的方法 默认是使用 pip install django 最新版的django会提示 要求python版本3.4以上,系统默认的版本是2.7.5 ...