1:Selenium中对浏览器的操作

首先生成一个Web对象

IWebDriver driver = new FirefoxDriver();
//打开指定的URL地址
driver.Navigate().GoToUrl(@"http://12.99.102.196:9080/corporbank/logon_pro.html");
//关闭浏览器
Driver.quit(); 

网银浏览器兼容性测试过程中,关闭浏览器后会有对话框,此问题解决方法如下:

public void logout()
{
System.Diagnostics.Process[] myProcesses;
       myProcesses = System.Diagnostics.Process.GetProcessesByName("IEXPLORE");
foreach (System.Diagnostics.Process instance in myProcesses)
{
instance.Kill();
}

2:Selenium中执行JS脚本

//需要将driver强制转换成JS执行器类型
((IJavaScriptExecutor) driver).ExecuteScript("js文件名"); 

3:Selenium中定位页面元素

driver.FindElement(By.Id("cp1_btnModify")).click();
By.ClassName(className));
By.CssSelector(selector) ;
By.Id(id);
By.LinkText(linkText);
By.Name(name);
By.PartialLinkText(linkText);
By.TagName(name);
By.Xpath(xpathExpression); 

3.1根据元素id定位并操作

//向指定的文本框输入字符串500001
Driver.FindElement(By.Id("amount")).SendKeys(""); 

3.2根据元素classname定位并操作

//点击classname为指定值的按钮
Driver.FindElement(By.ClassName(“WotherDay”)).click(); 

3.3根据元素的linktext定位并操作

Driver.FindElement(By.LinkText(“选择账号”)).click(); 

3.4根据元素的Name定位并操作

Driver.FindElement(By.Name(“quality”)).perform(); 

3.5使用CssSelector定位并操作

string order = "#resultTable.result_table tbody tr.bg1 td.center a";
driver.FindElement (By.CssSelector(order)).click(); 

3.6使用Xpath定位并元素并操作

//使用多个属性定位元素
Driver.FindElement(By.XPath("//input[@id='submit' and @value='下一步']")).click();
//使用绝对路径定位元素
string path = "/html/body/div[4]/div/div/div[2]/table/tbody/tr/td/a";
Driver.FindElement(By.Xpath(path)).click(); 

各方法使用优先原则:

优先使用id,name,classname,link;次之使用CssSelector();最后使用Xpath();

因为Xpath()方法的性能和效率最低下。

4:Selenium中清空文本框中的默认内容

//清空文本框clear()
Driver.FindElement(By.Id("tranAmtText")).clear();

5:Selenium中在指定的文本框中输入指定的字符串

//在文本框中输入指定的字符串sendkeys()
Driver.FindElement(By.Id("tranAmtText")).SendKeys(“”); 

6:Selenium中移动光标到指定的元素上

//移动光标到指定的元素上perform
Actions action=new Actions(driver);
action.MoveToElement(Find(By.XPath("//input[@id='submit' and @value='确定']"))).Perform(); 

7:Selenium中点击按钮/链接

//点击按钮/链接click()
Driver.FindElement(By.XPath("//input[@id='submit' and @value='下一步']")).click(); 

8:Selenium中等待页面上的元素加载完成

//等待页面元素加载完成
//默认等待100秒
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds());
//等待页面上ID属性值为submitButton的元素加载完成
wait.Until((d) => { return WaitForObject(By.Id("submitButton")); }); 

9:Selenium中模拟鼠标晃动

//模拟光标晃动movebyoffset()
Actions action = new Actions(driver);
action.MoveByOffset(, ); 

10:Selenium中对iframe中元素的定位

5.1:切换焦点到id为固定值的iframe上

进入页面后,光标默认焦点在DefaultContent中,若想要定位到iframe 需要转换焦点

driver.SwitchTo().DefaultContent();
//切换焦点到mainFrame
driver.SwitchTo().Frame("mainFrame"); 

需要注意的是:切换焦点之后若想切换焦点到其他iframe上 需要先返回到defaultcontent,再切换焦点到指定的iframe上。

5.2切换焦点到id值为动态值的iframe上

有时候 页面上浮出层的id为动态值,此时需要先获取所有符合记录的iframe放置在数组中,然后遍历数组切换焦点到目标iframe上。

如下方法:

protected string bizFrameId = string.Empty;
protected string bizId = string.Empty;
//获取动态iframe的id值
protected void SetIframeId()
{
ReadOnlyCollection<IWebElement> els = driver.FindElements(By.TagName("iframe"));
foreach (var e in driver.FindElements(By.TagName("iframe")))
{
string s1 = e.GetAttribute("id");
if (s1.IndexOf("window") >= && s1.IndexOf("content") >= )
{
bizFrameId = e.GetAttribute("id");
string[] ss = s1.Split(new char[] { '_' });
bizId = ss[];
}
}

11:Selenium中关闭多个子Browser窗口

//获取所有的WindowHandle,关闭所有子窗口
string oldwin = driver.CurrentWindowHandle;
ReadOnlyCollection<string> windows = driver.WindowHandles;
foreach (var win in windows)
{
if (win != oldwin)
{
driver.SwitchTo().Window(win).Close();
}
}
driver.SwitchTo().Window(oldwin); 

12:Selenium中对下拉框的操作

//选择下拉框
protected void SelectUsage(string selectid, string text)
{
IWebElement select = Find(By.Id(selectid));
IList<IWebElement> AllOptions = select.FindElements(By.TagName("option"));
foreach (IWebElement option in select.FindElements(By.TagName("option")))
{
if (option.GetAttribute("value").Equals(text))
option.Click();
}
}

13:Selenium中对confirm ,alert ,prompt的操作

//在本次浏览器兼容性测试项目中遇到的只有confirm和alert
//下面举例说明confirm和alert的代码,prompt类似
//confirm的操作
IAlert confirm = driver.SwitchTo().Alert();
confirm.Accept();
//Alert的操作
//个人网银中同样的业务有时候不会弹对alert,此时需要判断alert是否存在
//对Alert提示框作确定操作,默认等待50毫秒
protected void AlertAccept()
{
AlertAccept(0.05);
} //等待几秒,可以为小数,单位为秒
protected void AlertAccept(double waitseSonds)
{
double nsleepMillon = waitseSonds * ;
int k=;
int split=;
IAlert alert = null;
do
{
k++;
Thread.Sleep(split);
alert = driver.SwitchTo().Alert();
}
while (k * split <= nsleepMillon || alert==null);
if (alert != null)
{
alert.Accept();
}

14:Selenium WebDriver的截图功能

//WebDriver中自带截图功能
Screenshot screenShotFile = ((ITakesScreenshot)driver).GetScreenshot();
screenShotFile.SaveAsFile("test",ImageFormat.Jpeg);

Selenium+C#自动化脚本开发学习的更多相关文章

  1. Selenium自动化脚本开发总结

    Selenium Selenium 是ThoughtWorks专门为Web应用程序编写的一个验收测试工具. Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mo ...

  2. Auto.js无障碍免root脚本开发学习

    Auto.js 简单入门 官方文档:https://hyb1996.github.io/AutoJs-Docs/#/ https://blog.csdn.net/QiHsMing/article/de ...

  3. 关于selenium RC的脚本开发

    第一.需要录制脚本,找个我也不说了.就是在firefox下下载一个selenium-IDE并且安装. 第二.在工具里找到selenium-IDE点击运行. 第三.默认是红色按钮点击状态的,接下来随便你 ...

  4. Unity3D C#脚本开发学习

    1. Inherit from MonoBehaviour,All behaviour scripts must inherit from MonoBehaviour (directly or ind ...

  5. 【功耗测试环境预置自动化脚本开发】【切换wifi模式为siso模式】【用到方法*args】

    import os,reimport logginglogging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[ ...

  6. selenium-第一个自动化脚本

    经过上一篇的环境搭建,这一篇我们开始编写第一个自动化脚本. 一个简单的测试百度的demo #coding=utf-8 from selenium import webdriver driver = w ...

  7. Selenium2学习-001-Selenium2 WebUI自动化Java开发 Windows 环境配置

    此文主要介绍 Selenium2 WebUI自动化Java开发 Windows 环境配置,供各位亲们参考,若有不足之处,敬请各位大神指正,非常感谢! 所需软件列表如下所示: 所属分类 具体名称 备注 ...

  8. 《手把手教你》系列基础篇(五)-java+ selenium自动化测试- 创建首个自动化脚本(详细教程)

    1.简介 前面几篇宏哥介绍了两种(java和maven)环境搭建和三大浏览器的启动方法,这篇文章宏哥将要介绍第一个自动化测试脚本.前边环境都搭建成功了,浏览器也驱动成功了,那么我们不着急学习其他内容, ...

  9. java+selenium自动化脚本编写

    实训项目:创盟后台管理,页面自动化脚本编写 使用工具:java+selenium 1)java+selenium环境搭建文档 2)创盟项目后台管理系统链接 java+selenium环境搭建 一.Se ...

随机推荐

  1. 2017.4.18 静态代码分析工具sonarqube+sonar-runner的安装配置及使用

    配置成功后的代码分析页面: 可以看到对复杂度.语法使用.重复度等等都做了分析,具体到了每一个方法和每一句代码. 四种使用方式: sonarqube + sonar-runner sonarqube + ...

  2. [React] Use react-rewards to add microinteractions to React app to reward users for some actions

    It's important that our users enjoy using our application or website. One way we can make it happen ...

  3. 倍福TwinCAT(贝福Beckhoff)基础教程 松下伺服驱动器报错 88怎么办

    请确认在TWINCAT在线模式下,把Drive的Modes of operation改为8       更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com ...

  4. vscode - 更改emmet生成代码

    有时候生成的代码,并不适用自己,所以想想改生成代码: 因为windows查找文件/文件内容非常慢,所以借用了一下Linux的搜索命令,查找了一下 ie=edge  ,最后,找到了 expand-ful ...

  5. Java中的split函数的用法

    Java中的 split  函数是用于按指定字符(串)或正则去分割某个字符串,结果以字符串数组形式返回: 例如: String str="1234@abc"; String[] a ...

  6. android ListView滚动条监听判断滚动到底部还是顶部

    代码: lv.setOnScrollListener(new OnScrollListener() { public void onScrollStateChanged(AbsListView vie ...

  7. Storm/Cassandra集成错误:NoSuchMethodError: concurrent.Futures.withFallback

    本文原文出处: http://blog.csdn.net/bluishglc/article/details/50443205 严禁不论什么形式的转载.否则将托付CSDN官方维护权益. 2015年的最 ...

  8. mac 下 pycharm 快捷键

    用过快捷键立即感觉高大上了,最主要的是很方便啊!很强大 cmd b 跳转到声明处(cmd加鼠标) opt + 空格 显示符号代码 (esc退出窗口 回车进入代码) cmd []光标之前/后的位置 op ...

  9. ACM算法整理(不断补充ing)

    动态规划 1.背包问题 (1)01背包 ,n) DFR(v,V,C[i]) F[v]=max(F[v],F[v-C[i]]+W[i]); } //初始化时 //若背包不一定装满F全初始化为0 //若装 ...

  10. servlet文件下载2(单文件下载和批量下载)

    使用servlet完毕单文件下载和批量文件下载.批量下载的原理是先将文件打包成zip , 然后再下载. 之前也转载过一篇文件下载的博客,地址:http://blog.csdn.net/ch717828 ...