Selenium WebDriver 学习笔记
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 学习笔记的更多相关文章
- selenium webdriver 学习笔记(三)
selenium webdriver 一.上传文件操作 上传文件夹一般要打开一个本地窗口,从窗口选择本地文件添加.所以一般会卡在如何操作本地窗口添加上传文件. 其实,在selenium webdriv ...
- selenium webdriver 学习笔记(二)
selenium webdriver 一.定位一组元素: webdriver可以很方便的使用findElement 方法来定位某个物定的对象.不过有时候我们却要定位一组对象,这时候就需要使用findE ...
- selenium webdriver 学习笔记(一)
selenium webdriver 第一个脚本: #coding = utf-8 from selenium import webdriver import time url = "htt ...
- Python3+Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )
!/usr/bin/env python -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )'''from selenium im ...
- Python3+Selenium3+webdriver学习笔记13(js操作应用:弹出框无效如何处理)
#!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记13(js操作应用:弹出框无效如何处理)'''from sel ...
- Python3+Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)
#!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)'''from ...
- Python3+Selenium3+webdriver学习笔记11(cookie处理)
#!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记11(cookie处理)'''from selenium im ...
- Python3+Selenium3+webdriver学习笔记10(元素属性、页面源码)
#!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记10(元素属性.页面源码)'''from selenium i ...
- Python3+Selenium3+webdriver学习笔记9(发送富文本信息及上传文件处理)
#!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记9(发送富文本信息及上传文件处理)'''from seleni ...
随机推荐
- Javascript基础form表单
<!DOCTYPE HTML> <html> <head> <script type="text/javascript" charset= ...
- Code First 数据注释--InverseProperty 和 ForeignKey
ForeignKey 按照约定在Post类中看到BlogId属性,会认为是Blog类的外键,但是在Blog类中并没有BlogId属性,解决方法是,在 Post 中创建一个导航属性,并使用 Foreig ...
- 无法从带有索引像素格式的图像创建graphics对象(转)
大家在用 .NET 做图片水印功能的时候, 很可能会遇到 “无法从带有索引像素格式的图像创建graphics对象”这个错误,对应的英文错误提示是“A Graphics object cannot be ...
- 一个Banner广告收缩效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- UIButton和UIImageView的区别
1.显示图片 1> UIImageView只能一种图片(图片默认会填充整个UIImageView) image\setImage: 2> UIButton能显示2种图片 * 背景 (背景 ...
- HDU 5904 - LCIS (BestCoder Round #87)
HDU 5904 - LCIS [ DP ] BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式 ...
- C++中的虚函数
代码: #include <iostream> #include <cstring> using namespace std; class Base{ public: virt ...
- iphone手机端图片错位修正的js代码
<script type="text/javascript"> $(function(){ // 获取终端的相关信息 var Terminal = { // 辨别移动终 ...
- 关于Windows8.1更新后Sql Server服务消失的处理办法
前言 微软在17日发布了windows8.1,兴致勃勃地花了半天的时间更新了,不过不知所云的是,在20日又被卸下Windows Store.此为背景. 影响 更新完毕做开发的时候,发现SqlServe ...
- [POJ] 1064 Cable master (二分查找)
题目地址:http://poj.org/problem?id=1064 有N条绳子,它们的长度分别为Ai,如果从它们中切割出K条长度相同的绳子,这K条绳子每条最长能有多长. 二分绳子长度,然后验证即可 ...