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. Javascript进阶篇——(DOM—节点---插入、删除和替换元素、创建元素、创建文本节点)—笔记整理

    插入节点appendChild()在指定节点的最后一个子节点列表之后添加一个新的子节点.语法: appendChild(newnode) //参数: //newnode:指定追加的节点. 为ul添加一 ...

  2. SQL Server 创建链接服务器

    遇到下列问题: 线上服务器A,中转服务器B,本地服务器C 数据在A上面,想在B上面操作类似 select * from [A].[database].table这样的SQL,不用去链接服务器,直接把处 ...

  3. sqlserver插入datetime

    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")

  4. .net 调用Oracle.Data.Access 组件提供的用于批量操作的方法—获取数据库表结构方法和跟参数赋值方法

    1./// <summary> /// 获取当前目标表结构 /// </summary> /// <param name="tableName"> ...

  5. Python爬虫——抓取贴吧帖子

    抓取百度贴吧帖子 按照这个学习教程,一步一步写出来,中间遇到很多的问题,一一列举 首先, 获得 标题 和 贴子总数 # -*- coding:utf-8 -*- #!/user/bin/python ...

  6. 流输入练习——寻找Sb.VI codevs 3096

    题目描述 Description 已知某开放授权人员名叫Serb,由于经常修改各种数据,因此开发人员们都喊他SB.现在他和许多人一起过飞机安检,排成了一长队列,请问SB.是否在队列中. 输入描述 In ...

  7. Java学习笔记——可视化Swing中JTable控件绑定SQL数据源的两种方法

    在 MyEclipse 的可视化 Swing 中,有 JTable 控件. JTable 用来显示和编辑常规二维单元表. 那么,如何将 数据库SQL中的数据绑定至JTable中呢? 在这里,提供两种方 ...

  8. php+mysql将大数据sql文件导入数据库

    <?php $file_name = "d:test.sql"; $dbhost = "localhost"; $dbuser = "root& ...

  9. HBase -ROOT-和.META.表结构(region定位原理)

    在HBase中,大部分的操作都是在RegionServer完成的,Client端想要插入,删除,查询数据都需要先找到相应的RegionServer.什么叫相应的RegionServer?就是管理你要操 ...

  10. ecshop优化修改sitemap.xml到根目录

    大家都知道sitemap.xml是用来给搜索引擎提交收录的工具,虽然搜索引擎自己也会收录网站但是有了sitemap.xml之后速度会加快不少.而ecshop程序是有自动生成sitemap.xml的功能 ...