Selenium WebDriver 学习笔记

时间:2023-03-09 04:12:50
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();
}
}
}
}