C# 利用Selenium实现浏览器自动化操作的示例代码
概述
Selenium是一款免费的分布式的自动化测试工具,支持多种开发语言,无论是C、java、ruby、python、或是C#,你都可以通过selenium完成自动化测试。本文以一个简单的小例子,简述C#利用Selenium进行浏览器的模拟操作,仅供学习分享使用,如有不足之处,还请指正。
涉及知识点
要实现本例的功能,除了要掌握Html,JavaScript,CSS等基础知识,还涉及以下知识点:
- log4net:主要用于日志的记录和存储,本例采用log4net进行日志记录,便于过程跟踪和问题排查,关于log4net的配置和介绍,之前已有说明,本文不做赘述。
- Queue:队列,先进先出模式,本文主要用于将日志信息保存于队列中,然后再显示到页面上,其中Enqueue用于添加内容到结尾处,Dequeue用于返回并移除一个位置的对象。
- IWebDriver:浏览器驱动接口,所有的关于浏览器的操作都可以通过此接口进行,不同浏览器有不同的实现类,如:IE浏览器(InternetExplorerDriver)Chrome浏览器(ChromeDriver)等。
- BackgroundWorker:后台工作线程,区别于主线程,通过事件触发不同的状态。
Selenium安装
本例开发工具为VS2019,通过NuGet进行需要的软件包的安装与管理,如下所示:
示例效果图
本例采用Chrome浏览器,用于监控某一个网站并获取相应内容,如下所示:
Selenium示例介绍
定义一个webDriver,如下所示:
//谷歌浏览器 ChromeOptionsoptions=newChromeOptions(); this.driver=newChromeDriver(options);
通过ID获取元素并填充内容和触发事件,如下所示:
this.driver.FindElement(By.Id("email")).SendKeys(username);
this.driver.FindElement(By.Id("password")).SendKeys(password);
//#7.点击登录按钮
this.driver.FindElement(By.Id("sign-in")).Click();
通过XPath获取元素,如下所示:
stringxpath1="//div[@class=\"product-list\"]/div[@class=\"product\"]/div[@class=\"price-and-detail\"]/div[@class=\"price\"]/span[@class=\"noStock\"]"; stringtxt=this.driver.FindElement(By.XPath(xpath1)).Text;
核心代码
主要的核心代码,就是浏览器的元素定位查找和事件触发,如下所示:
usingOpenQA.Selenium;
usingOpenQA.Selenium.IE;
usingOpenQA.Selenium.Chrome;
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading;
usingSystem.Threading.Tasks;
namespaceAiSmoking.Core
{
publicclassSmoking
{
///
///是否正在运行
///
privateboolrunning=false;
///
///驱动
///
privateIWebDriverdriver=null;
///
///#无货
///
privatestringno_stock="CurrentlyOutofStock";
///
///#线程等待秒数
///
privateintwait_sec=2;
privateDictionarycfg_info;
privatestringwork_path=string.Empty;
///
///构造函数
///
publicSmoking()
{
}
///
///带参构造函数
///
///
///
publicSmoking(Dictionarycfg_info,stringwork_path)
{
this.cfg_info=cfg_info;
this.work_path=work_path;
this.wait_sec=int.Parse(cfg_info["wait_sec"]);
//#如果小于2,则等于2
this.wait_sec=(this.wait_sec<2?2:this.wait_sec);
this.wait_sec=this.wait_sec*1000;
}
///
///开始跑
///
publicvoidstartRun()
{
//"""运行起来"""
try
{
this.running=true;
stringurl=this.cfg_info["url"];
stringusername=this.cfg_info["username"];
stringpassword=this.cfg_info["password"];
stringitem_id=this.cfg_info["item_id"];
if(string.IsNullOrEmpty(url)||string.IsNullOrEmpty(username)||string.IsNullOrEmpty(password)||string.IsNullOrEmpty(item_id))
{
LogHelper.put("配置信息不全,请检查config.cfg文件是否为空,然后再重启");
return;
}
if(this.driver==null)
{
stringexplorer=this.cfg_info["explorer"];
if(explorer=="Chrome")
{
//谷歌浏览器
ChromeOptionsoptions=newChromeOptions();
this.driver=newChromeDriver(options);
}
else
{
//默认IE
varoptions=newInternetExplorerOptions();
//options.AddAdditionalCapability.('encoding=UTF-8')
//options.add_argument('Accept=text/css,*/*')
//options.add_argument('Accept-Language=zh-Hans-CN,zh-Hans;q=0.5')
//options.add_argument('Accept-Encoding=gzip,deflate')
//options.add_argument('user-agent=Mozilla/5.0(WindowsNT10.0;WOW64;Trident/7.0;rv:11.0)likeGecko')
//#2.定义浏览器驱动对象
this.driver=newInternetExplorerDriver(options);
}
}
this.run(url,username,password,item_id);
}
catch(Exceptione)
{
LogHelper.put("运行过程中出错,请重新打开再试"+e.StackTrace);
}
}
///
///运行
///
///
///
///
///
privatevoidrun(stringurl,stringusername,stringpassword,stringitem_id)
{
//"""运行起来"""
//#3.访问网站
this.driver.Navigate().GoToUrl(url);
//#4.最大化窗口
this.driver.Manage().Window.Maximize();
if(this.checkIsExists(By.LinkText("账户登录")))
{
//#判断是否登录:未登录
this.login(username,password);
}
if(this.checkIsExists(By.PartialLinkText("欢迎回来")))
{
//#判断是否登录:已登录
LogHelper.put("登录成功,下一步开始工作了");
this.working(item_id);
}
else
{
LogHelper.put("登录失败,请设置账号密码");
}
}
///
///停止运行
///
publicvoidstopRun()
{
//"""停止"""
try
{
this.running=false;
//#如果驱动不为空,则关闭
//self.close_browser_nicely(self.__driver)
if(this.driver!=null)
{
this.driver.Quit();
//#关闭后切要为None,否则启动报错
this.driver=null;
}
}
catch(Exceptione)
{
//print('StopFailure')
}
finally
{
this.driver=null;
}
}
privatevoidlogin(stringusername,stringpassword)
{
//#5.点击链接跳转到登录页面
this.driver.FindElement(By.LinkText("账户登录")).Click();
//#6.输入账号密码
//#判断是否加载完成
if(this.checkIsExists(By.Id("email")))
{
this.driver.FindElement(By.Id("email")).SendKeys(username);
this.driver.FindElement(By.Id("password")).SendKeys(password);
//#7.点击登录按钮
this.driver.FindElement(By.Id("sign-in")).Click();
}
}
///
///工作状态
///
///
privatevoidworking(stringitem_id)
{
while(this.running)
{
try
{
//#正常获取信息
if(this.checkIsExists(By.Id("string")))
{
this.driver.FindElement(By.Id("string")).Clear();
this.driver.FindElement(By.Id("string")).SendKeys(item_id);
this.driver.FindElement(By.Id("string")).SendKeys(Keys.Enter);
}
//#判断是否查询到商品
stringxpath="//div[@class=\"specialty-headersearch\"]/div[@class=\"specialty-description\"]/div[@class=\"gt-450\"]/span[2]";
if(this.checkIsExists(By.XPath(xpath)))
{
intcount=int.Parse(this.driver.FindElement(By.XPath(xpath)).Text);
if(count<1)
{
Thread.Sleep(this.wait_sec);
LogHelper.put("没有查询到itemid="+item_id+"对应的信息");
continue;
}
}
else
{
Thread.Sleep(this.wait_sec);
LogHelper.put("没有查询到itemid2="+item_id+"对应的信息");
continue;
}
//#判断当前库存是否有货
stringxpath1="//div[@class=\"product-list\"]/div[@class=\"product\"]/div[@class=\"price-and-detail\"]/div[@class=\"price\"]/span[@class=\"noStock\"]";
if(this.checkIsExists(By.XPath(xpath1)))
{
stringtxt=this.driver.FindElement(By.XPath(xpath1)).Text;
if(txt==this.no_stock)
{
//#当前无货
Thread.Sleep(this.wait_sec);
LogHelper.put("查询一次"+item_id+",无货");
continue;
}
}
//#链接path1
stringxpath2="//div[@class=\"product-list\"]/div[@class=\"product\"]/div[@class=\"imgDiv\"]/a";
//#判断是否加载完毕
//#this.waiting((By.CLASS_NAME,"imgDiv"))
if(this.checkIsExists(By.XPath(xpath2)))
{
this.driver.FindElement(By.XPath(xpath2)).Click();
Thread.Sleep(this.wait_sec);
//#加入购物车
if(this.checkIsExists(By.ClassName("add-to-cart")))
{
this.driver.FindElement(By.ClassName("add-to-cart")).Click();
LogHelper.put("加入购物车成功,商品item-id:"+item_id);
break;
}
else
{
LogHelper.put("未找到加入购物车按钮");
}
}
else
{
LogHelper.put("没有查询到,可能是商品编码不对,或者已下架");
}
Thread.Sleep(this.wait_sec);
}
catch(Exceptione)
{
Thread.Sleep(this.wait_sec);
LogHelper.put(e);
}
}
}
///
///判断是否存在
///
///
///
privateboolcheckIsExists(Byby)
{
try
{
inti=0;
while(this.running&&i<3)
{
if(this.driver.FindElements(by).Count>0)
{
break;
}
else
{
Thread.Sleep(this.wait_sec);
i=i+1;
}
}
returnthis.driver.FindElements(by).Count>0;
}
catch(Exceptione)
{
LogHelper.put(e);
returnfalse;
}
}
}
}
关于日志帮助类,代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usinglog4net;
[assembly:log4net.Config.XmlConfigurator(Watch=true)]
namespaceAiSmoking.Core
{
///
///日志帮助类
///
publicstaticclassLogHelper
{
///
///日志实例
///
privatestaticILoglogInstance=LogManager.GetLogger("smoking");
privatestaticQueuequeue=newQueue(2000);
publicstaticvoidput(stringmsg)
{
queue.Enqueue(msg);
WriteLog(msg,LogLevel.Info);
}
publicstaticvoidput(Exceptionex)
{
WriteLog(ex.StackTrace,LogLevel.Error);
}
publicstaticstringget()
{
if(queue.Count>0)
{
returnqueue.Dequeue();
}
else
{
returnstring.Empty;
}
}
publicstaticvoidWriteLog(stringmessage,LogLevellevel)
{
switch(level)
{
caseLogLevel.Debug:
logInstance.Debug(message);
break;
caseLogLevel.Error:
logInstance.Error(message);
break;
caseLogLevel.Fatal:
logInstance.Fatal(message);
break;
caseLogLevel.Info:
logInstance.Info(message);
break;
caseLogLevel.Warn:
logInstance.Warn(message);
break;
default:
logInstance.Info(message);
break;
}
}
}
publicenumLogLevel
{
Debug=0,
Error=1,
Fatal=2,
Info=3,
Warn=4
}
}
作者:Alan.hsiang
出处:http://www.cnblogs.com/hsiang/
以上就是C#利用Selenium实现浏览器自动化操作的示例代码的详细内容,更多关于c#实现浏览器自动化操作的资料请关注毛票票其它相关文章!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。
热门推荐
10 广西考试祝福语结婚简短
11 猪年祝福语简短小孩
12 元旦祝福语送长辈简短
13 恭喜二宝祝福语简短
14 祝福语暖心话简短
15 国庆中秋祝福语简短兄弟
16 朋友订婚的祝福语简短
17 送弟弟中秋祝福语简短
18 爱生日祝福语简短独特