c seleniumm actions类是c#中的吗

Selenium2.0功能测试之如何使用Action类来模拟交互
Selenium2.0功能测试之如何使用Action类来模拟交互
  Selenium提供了一个强大的用于真实的模拟用户交互的一个类----Actions,这个类提共了一系列的API供模拟交互:  keyDown : 用于模拟按键被按下  keyUp : 用于模拟按键松开  doubleClick : 用于模拟双击  clickAndHold : 用于模拟鼠标左键点住不放开  release : 用于模拟松开鼠标,与clickAndHold相配合  moveToElement : 将鼠标移动至元素的中间位置  contextClick : 模拟鼠标右键点击  dragAndDrop : 拖拽  这里由于测试页面的限制我就只举一个contextClick的例子:    package derinfo.  import org.openqa.selenium.By;  import org.openqa.selenium.WebD  import org.openqa.selenium.WebE  import org.openqa.selenium.chrome.ChromeD  import org.openqa.selenium.interactions.A  public class ActionDemo {  private static final String URL = "";  /**  * @author Coderinfo  * @E-mail   */  public static void main(String[] args) throws InterruptedException {  WebDriver driver = new ChromeDriver();  driver.manage().window().maximize(); //最大化浏览器界面  driver.get(URL); //访问度娘首页。  Thread.sleep(2000); //等待页面加载  WebElement input = driver.findElement(By.id("kw")); &//获取百度搜索框  Actions ac = new Actions(driver); &// 为driver 加载 actions  ntextClick(input).perform(); &// 在百度搜索框上点击右键  Thread.sleep(10000);  driver.quit();  }  }相关文章:  Selenium2.0功能测试之文件上传(Java版)
&&&主编推荐
H3C认证Java认证Oracle认证
基础英语软考英语项目管理英语职场英语
.NETPowerBuilderWeb开发游戏开发Perl
二级模拟试题一级模拟试题一级考试经验四级考试资料
软件测试软件外包系统分析与建模敏捷开发
法律法规历年试题软考英语网络管理员系统架构设计师信息系统监理师
高级通信工程师考试大纲设备环境综合能力
路由技术网络存储无线网络网络设备
CPMP考试prince2认证项目范围管理项目配置管理项目管理案例项目经理项目干系人管理
职称考试题目
招生信息考研政治
网络安全安全设置工具使用手机安全
生物识别传感器物联网传输层物联网前沿技术物联网案例分析
Java核心技术J2ME教程
Linux系统管理Linux编程Linux安全AIX教程
Windows系统管理Windows教程Windows网络管理Windows故障
数据库开发Sybase数据库Informix数据库
&&&&&&&&&&&&&&&
希赛网 版权所有 & &&自动化测试:Selenium webdriver 学习笔记-C#版(四) - Alvin-x - 博客园
  前面我们知道了如何进行对象的定位,下面我们进一步来了解selenium的一些功能特性:
1&等待:我们在处理对象的时候,对象并不能及时的&出现&,那么此时我们就需要进行等待了。
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(20)); //这里的20,是以"s" 为单位,这里的数值可以根据实际情况来设置,
&&&& 还有一种等待方式:使用WebDriverWait对象,同样强大。
public IWebElement WaitForElement(IWebDriver driver, string el_id,int timeout)
//找到元素就返回
IWebElement ele = null;
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
ele = wait.Until&IWebElement&((d) =&
return d.FindElement(By.Id(el_id));
Console.WriteLine("12e");
2&获取窗体句柄:
driver.CurrentWindowHandle
3&窗体转换:
driver.SwitchTo().Window();
4&使用Actions
var xx = driver.FindElement(By.Id("id"));
Actions builder = new Actions(driver);
builder.MoveToElement(xx).Perform();
5&根据链接文本定位对象(不太适用本地化测试)
driver.FindElement(By.PartialLinkText("登录"));
6&处理弹出框
driver.SwitchTo().Alert().Accept()//简单讲就是点击yes
driver.SwitchTo().Alert().Dismiss()//点击No
&7&判断页面字符串
driver.PageSource.Contains("");
下面来看具体的实例:
登陆百度首页-&搜索&博客园&-&登陆博客园-&退出-&处理弹出框
using System.Collections.G
using System.L
using System.T
using System.T
//添加selenium的引用
using OpenQA.S
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.I
//添加引用-在程序集中添加System.Drawing
using System.D
using System.Drawing.I
namespace Selenium
class Program
static void Main(string[] args)
//此时记得添加路径
using (var driver = new InternetExplorerDriver(@"D:\Selenium\IEDriverServer_x64_2.34.0\"))
//进入百度首页
driver.Navigate().GoToUrl(@"");
Thread.Sleep(1000);
//是否包含"百度"这个字符串,可以用来判断页面是否出现
if (driver.PageSource.Contains("百度")){ Console.WriteLine(" 123");}
//设置窗体最大化
driver.Manage().Window.Maximize();
Thread.Sleep(1000);
//找到对象
var colSearchBox = driver.FindElementsByName("wd");
var btnClick = driver.FindElement(By.Id("su1"));
//发送搜索内容
colSearchBox[1].SendKeys("bokeyuan");
//Thread.Sleep(1000);
//等待搜索结果
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement searchResutl = null;
searchResutl = wait.Until&IWebElement&((d) =&
return d.FindElement(By.ClassName("bdsug"));
catch(Exception e)
Console.WriteLine("Timeout to find element:" + " "+ e.Message.ToString());
//搜索结果数量
var searchResult_Children = searchResutl.FindElements(By.TagName("li"));
foreach (IWebElement child in searchResult_Children)
if (child.Text.Equals("博客园"))
//选择正确的搜索对象
child.Click();
//设置页面加载时间
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(20));
//获取当前页面句柄 ,适用于一个窗体
//var cc = driver.CurrentWindowH
//进入首页
var homePage = driver.FindElement(By.ClassName("result"));
var homePage_child = homePage.FindElement(By.Id("1"));
homePage_child.FindElement(By.ClassName("favurl")).Click();
//设置页面加载时间
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(20));
//获取当前网页的句柄,使用与多个窗体
//那么我们需要的是第二个窗体
var currentWindowHandle = driver.WindowHandles[1];
//因为要现在要处理的对象在新窗体上,所以这里要进行窗体转换
driver.SwitchTo().Window(currentWindowHandle);
//下面将鼠标移动到左边的".Net技术",此时会滑出相应的模块
//用XPath定位对象,此处找到"新手区"
//移动鼠标
var xx = driver.FindElement(By.Id("cate_item_108698"));
Actions builder = new Actions(driver);
builder.MoveToElement(xx).Perform();
Thread.Sleep(2000);
//使用XPath 找对象
//driver.FindElement(By.Id("cate_sub_block")).FindElement(By.XPath("//div[1]/div[2]/ul[1]/li[1]/a[@href='/cate/beginner/']")).Click();
//div[1]第一个div
//上面的比较"笨重",下面的简单多了
driver.FindElement(By.Id("cate_sub_block")).FindElement(By.XPath("//a[@href='/cate/beginner/']")).Click();
//使用PartialLinkText定位对象
var btnLogin1 = driver.FindElement(By.PartialLinkText("登录"));
btnLogin1.Click();
var txtUserName = driver.FindElement(By.Id("tbUserName"));
txtUserName.SendKeys("Alvin-x");
var txtPassword = driver.FindElement(By.Id("tbPassword"));
txtPassword.SendKeys("123456");
var btnLogin2 = driver.FindElement(By.Id("btnLogin"));
btnLogin2.Click();
Thread.Sleep(2000);
//使用CssSelector定位对象
//点击&退出&
var btnBackup = driver.FindElement(By.CssSelector("a[href='#']"));
btnBackup.Click();
//等待弹出框弹出后再处理它
Thread.Sleep(1000);
IAlert result = null;
while (1 & 2)
result = driver.SwitchTo().Alert();
catch (Exception)
result = null;
if (result != null)
result.Accept();
driver.Quit();关注51Testing
Selenium2.0功能测试之如何使用Action类来模拟交互
发表于: 11:57 &作者:CoderInfo & 来源:51Testing软件测试网采编
推荐标签:
  提供了一个强大的用于真实的模拟用户交互的一个类----Actions,这个类提共了一系列的API供模拟交互:  keyDown : 用于模拟按键被按下  keyUp : 用于模拟按键松开  doubleClick : 用于模拟双击  clickAndHold : 用于模拟鼠标左键点住不放开  release : 用于模拟松开鼠标,与clickAndHold相配合  moveToElement : 将鼠标至元素的中间位置  contextClick : 模拟鼠标右键点击  dragAndDrop : 拖拽  这里由于页面的限制我就只举一个contextClick的例子:package org.coderinfo.import org.openqa.selenium.By;import org.openqa.selenium.WebDimport org.openqa.selenium.WebEimport org.openqa.selenium.chrome.ChromeDimport org.openqa.selenium.interactions.Apublic class ActionDemo {private static final String URL = "";/*** @author Coderinfo* @E-mail */public static void main(String[] args) throws InterruptedException {WebDriver driver = new ChromeDriver();driver.manage().window().maximize(); //最大化浏览器界面driver.get(URL); //访问度娘首页。Thread.sleep(2000); //等待页面加载WebElement input = driver.findElement(By.id("kw")); &//获取搜索框Actions ac = new Actions(driver); &// 为driver 加载 actionsac.contextClick(input).perform(); &// 在百度搜索框上点击右键Thread.sleep(10000);driver.quit();}}相关:
搜索风云榜
51Testing官方微信
51Testing官方微博
测试知识全知道}

我要回帖

更多关于 selenium c 教程 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信