1. 打开VS2012

2. 新建工程(单元测试工程或控制台程序都可以, 看需求)

3. 工具->NuGet程序包管理器->程序包管理器控制台

4. 输入"Install-Package Selenium.WebDriver"安装程序包

5. 输入"Install-Package Selenium.WebDriverBackedSelenium"安装程序包

6. 输入"Install-Package Selenium.Support"安装程序包

一些方法的意义(主观感觉, 文档有限(并且国内访问不了), 一天左右的时间的接触):

//火狐控制器, 还有别的浏览器, 需要安装对应的Driver, 如ChromeDriver
IWebDriver driver = new FirefoxDriver(); //加载页面
driver.Navigate().GoToUrl(baseURL + "/"); //最大化窗口
driver.Manage().Window.Maximize(); //线程等待
System.Threading.Thread.Sleep(); //点击元素
driver.FindElement(By.Id("Login")).Click(); //填写表单
driver.FindElement(By.Id("poploginId")).SendKeys("xachary"); //获取鼠标动作
Actions actions = new Actions(driver); //把鼠标移动到元素
actions.MoveToElement(element); //移动坐标(根据上一次的相对位置)
actions.MoveByOffset(, ); //执行鼠标动作(包含多个动作)
actions.Perform(); //等待元素出现(关键是等待的元素是从不可见到可见, 不然无效, 则用Thread.Sleep吧)
driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan( * )); //用CSS选择查找元素
driver.FindElement(By.CssSelector("li.first > a > img")).Click();

例子片段:

using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Interactions.Internal;
using System.Drawing; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium.Chrome; namespace BosSeleniumDriver
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver;
string baseURL;
driver = new FirefoxDriver();
//driver = new ChromeDriver();
//baseURL = "http://localhost:49912";
baseURL = "http://172.16.8.7:4399";
try
{
//首页
driver.Navigate().GoToUrl(baseURL + "/"); //最大化窗口
driver.Manage().Window.Maximize(); //线程等待1秒
System.Threading.Thread.Sleep(); //登陆
driver.FindElement(By.Id("Login")).Click();
driver.FindElement(By.Id("poploginId")).SendKeys("xachary");
driver.FindElement(By.Id("password")).SendKeys("home890619");
driver.FindElement(By.Id("loginbtn")).Click(); //线程等待1秒
System.Threading.Thread.Sleep(); Actions actions = new Actions(driver);
//移动鼠标让导航出现
IWebElement nav = driver.FindElements(By.CssSelector(".has_sub"))[];
actions.MoveToElement(nav);
actions.MoveByOffset(, );
actions.Perform(); //等待导航出现, 100毫秒
driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan( * )); //点击家庭药箱链接
actions.Click();
actions.Perform(); //选择第一个商品
driver.FindElement(By.CssSelector("li.first > a > img")).Click(); //跳转到新页面
driver.SwitchTo().Window(driver.WindowHandles.Last()); //购买
driver.FindElement(By.LinkText("立即购买")).Click(); //等待获取收货地址
driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(, , )); //如果没有地址
if (driver.FindElements(By.CssSelector(".address-radio.fl")).Count == )
{
//级联下拉框
new SelectElement(driver.FindElement(By.Id("sltProvince"))).SelectByText("广东省"); driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(, , )); new SelectElement(driver.FindElement(By.Id("sltCity"))).SelectByText("珠海市"); driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(, , )); new SelectElement(driver.FindElement(By.Id("sltArea"))).SelectByText("香洲区"); //填写表单
driver.FindElement(By.Id("txtAddress")).Clear();
driver.FindElement(By.Id("txtAddress")).SendKeys("测试地址");
driver.FindElement(By.Id("txtReceive")).Clear();
driver.FindElement(By.Id("txtReceive")).SendKeys("测试收货人");
driver.FindElement(By.Id("txtTelephone")).Clear();
driver.FindElement(By.Id("txtTelephone")).SendKeys("");
//提交表单
driver.FindElement(By.Id("btnUpdateAddr")).Click();
}
driver.FindElement(By.Name("rdoAddr")).Click(); //等待商品信息出现
WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(, , ));
wait.Until<IWebElement>((o) =>
{
return driver.FindElement(By.CssSelector(".cart_list_td tr"));
}); //等待提交订单按钮生效
System.Threading.Thread.Sleep();
//提交订单
driver.FindElement(By.LinkText("提交订单")).Click(); //选择工行支付
WebDriverWait wait2 = new WebDriverWait(driver, new TimeSpan(, , ));
wait2.Until<IWebElement>((o) =>
{
return driver.FindElement(By.Id("gh"));
}).Click();
//支付
driver.FindElement(By.Id("btn_pay")).Click();
driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(, , ));
//关掉支付弹出页
driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.Close();
//回到支付页面
driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.FindElement(By.LinkText("付款遇到问题")).Click();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("结束");
System.Threading.Thread.Sleep();
//Console.ReadLine();
driver.Quit();
}
}
}
}

Selenium WebDriver 学习笔记的更多相关文章

  1. selenium webdriver 学习笔记(三)

    selenium webdriver 一.上传文件操作 上传文件夹一般要打开一个本地窗口,从窗口选择本地文件添加.所以一般会卡在如何操作本地窗口添加上传文件. 其实,在selenium webdriv ...

  2. selenium webdriver 学习笔记(二)

    selenium webdriver 一.定位一组元素: webdriver可以很方便的使用findElement 方法来定位某个物定的对象.不过有时候我们却要定位一组对象,这时候就需要使用findE ...

  3. selenium webdriver 学习笔记(一)

    selenium webdriver 第一个脚本: #coding = utf-8 from selenium import webdriver import time url = "htt ...

  4. Python3+Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )

    !/usr/bin/env python -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )'''from selenium im ...

  5. Python3+Selenium3+webdriver学习笔记13(js操作应用:弹出框无效如何处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记13(js操作应用:弹出框无效如何处理)'''from sel ...

  6. Python3+Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)'''from ...

  7. Python3+Selenium3+webdriver学习笔记11(cookie处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记11(cookie处理)'''from selenium im ...

  8. Python3+Selenium3+webdriver学习笔记10(元素属性、页面源码)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记10(元素属性.页面源码)'''from selenium i ...

  9. Python3+Selenium3+webdriver学习笔记9(发送富文本信息及上传文件处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记9(发送富文本信息及上传文件处理)'''from seleni ...

随机推荐

  1. Application 可以存储全局变量

    Application.Lock(); Application["Name"]="小亮" Application.UnLock(); Response.Writ ...

  2. Android -------- 使手机状态栏背景颜色和activity的一致

    Activity类中: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInsta ...

  3. 我永远的 dell 15r

    陪伴我三年多的15r  让我疯狂过.努力过.更记录了我很多的成长,与很多个瞬间.看到它是有感情的.还记得第一次失去她.我好像失去了自己. 我是一个程序员.每个程序员都有自己的环境配置.不同的软件.就是 ...

  4. Struts2 技术全总结 (正在更新)

    背景:Struts1是一个高度成熟的框架,运行效率高,但其致命缺陷在于与JSP/Servlet的耦合非常紧密,因而导致了一些严重问题.其次,Struts1与Servlet API的严重耦合,使应用难以 ...

  5. 寻找子串位置 codevs 1204

    题目描述 Description 给出字符串a和字符串b,保证b是a的一个子串,请你输出b在a中第一次出现的位置. 输入描述 Input Description 仅一行包含两个字符串a和b 输出描述  ...

  6. java基础知识3

    58.线程的基本概念.线程的基本状态以及状态之间的关系线程指在程序执行过程中,能够执行程序代码的一个执行单位,每个程序至少都有一个线程,也就是程序本身.Java中的线程有四种状态分别是:运行.就绪.挂 ...

  7. 在shell中运行以不同方式运行脚本

    在shell当中,可以有3中方式运行脚本: 1 . ./script_name 或者source ./script_name 2 直接./script_name 3 ./script_name &am ...

  8. win8(64位)下memcache安装时报错“ failed to install service or service already installed” 与安装

    解决办法: 1.找到cmd.exe文件(c:\windows\system32\cmd.exe) 2.右键cmd.exe以管理员方式运行 3.把php_memcache.dll放到php的ext目录: ...

  9. Objective-C 计算代码运行时间

    今天看到一篇关于iOS应用性能优化的文章,其中提到计算代码的运行时间,觉得非常有用,值得收藏.不过在模拟器和真机上是有差异的,以此方法观察程序运行状态,提高效率. 第一种:(最简单的NSDate) N ...

  10. Cocos2d-x程序Windows下VC中文乱码的解决(用MultiByteToWideChar进行转换,VC2010有非常厉害的execution_character_set)

    Cocos2d-x默认字符串常量编码都是UTF8的,而Windows中的VC默认都是跟系统相同,比如简体Windows是GB2312或者GBK.繁体就是BIG5编码.而我们大多数中国人用VC编译出来的 ...