当前位置:首页 > 软件开放 > 正文内容

点击确定按钮跳转网页的代码(点击确定后将跳转至最新版下载页面)

软件开放3天前156

概述

Selenium是一款免费的分布式的自动化测试工具,支持多种开发语言,无论是C、 java、ruby、python、或是C# ,你都可以通过selenium完成自动化测试。本文以一个简单的小例子,简述C# 利用Selenium进行浏览器的模拟操作,仅供学习分享使用,如有不足之处,还请指正。

点击确定按钮跳转网页的代码(点击确定后将跳转至最新版下载页面)

涉及知识点

要实现本例的功能,除了要掌握Html ,Java,CSS等基础知识,还涉及以下知识点:

log4net:主要用于日志的记录和存储,本例采用log4net进行日志记录,便于过程跟踪和问题排查,关于log4net的配置和介绍,之前已有说明,本文不做赘述。

Queue:队列,先进先出模式,本文主要用于将日志信息保存于队列中,然后再显示到页面上,其中Enqueue用于添加内容到结尾处,Dequeue用于返回并移除一个位置的对象。

IWebDriver:浏览器驱动接口,所有的关于浏览器的操作都可以通过此接口进行,不同浏览器有不同的实现类,如:IE浏览器(InternetExplorerDriver)Chrome浏览器(ChromeDriver)等。

BackgroundWorker:后台工作线程,区别于主线程,通过事件触发不同的状态。

本例开发工具为VS2019,通过NuGet进行需要的软件包的安装与管理,如下所示:

示例效果图

本例采用Chrome浏览器,用于监控某一个网站并获取相应内容,如下所示:

展开全文

Selenium示例介绍

定义一个webDriver,如下所示:

通过ID获取元素并填充内容和触发事件,如下所示:

通过XPath获取元素,如下所示:

核心代码

主要的核心代码,就是浏览器的元素定位查找和事件触发,如下所示:

namespaceAiSmoking.Core{publicclassSmoking{///summary///是否正在运行 ////summaryprivateboolrunning = false;

///summary///驱动 ////summaryprivateIWebDriver driver = null;

///summary///# 无货 ////summaryprivatestringno_stock = "Currently Out of Stock";

///summary///# 线程等待秒数 ////summaryprivateintwait_sec = 2;

privateDictionary string, string cfg_info;

privatestringwork_path = string.Empty;

///summary///构造函数 ////summarypublicSmoking( ) {

}

///summary///带参构造函数 ////summary///param name="cfg_info"/param///param name="work_path"/parampublicSmoking( Dictionary string, string cfg_info, stringwork_path ) {this.cfg_info = cfg_info; this.work_path = work_path; this.wait_sec = int.Parse(cfg_info[ "wait_sec"]); //# 如果小于2,则等于2this.wait_sec = ( this.wait_sec 2? 2: this.wait_sec); this.wait_sec = this.wait_sec * 1000; }

///summary///开始跑 ////summarypublicvoidstartRun( ) {//"""运行起来"""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") {//谷歌浏览器ChromeOptions options = newChromeOptions; this.driver = newChromeDriver(options); }else{//默认IEvaroptions = 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 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko')//# 2. 定义浏览器驱动对象this.driver = newInternetExplorerDriver(options); }}this.run(url, username, password, item_id); }catch(Exception e) {LogHelper.put( "运行过程中出错,请重新打开再试"+e.StackTrace); }}

///summary///运行 ////summary///param name="url"/param///param name="username"/param///param name="password"/param///param name="item_id"/paramprivatevoidrun( 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( "登录失败,请设置账号密码"); }}

///summary///停止运行 ////summarypublicvoidstopRun( ) {//"""停止"""try{this.running = false; //# 如果驱动不为空,则关闭//self.close_browser_nicely(self.__driver)if( this.driver != null) {this.driver.Quit; //# 关闭后切要为None,否则启动报错this.driver = null; }}catch(Exception e) {//print('Stop Failure')}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; }}

///summary///工作状态 ////summary///param name="item_id"/paramprivatevoidworking( 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-header search\"]/div[@class=\"specialty-deion\"]/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( "没有查询到item id ="+ item_id + "对应的信息"); continue; }}else{Thread.Sleep( this.wait_sec); LogHelper.put( "没有查询到item id2 ="+ 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; }}//# 链接path1stringxpath2 = "//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(Exception e) {Thread.Sleep( this.wait_sec); LogHelper.put(e);}}}

///summary///判断是否存在 ////summary///param name="by"/param///returns/returnsprivateboolcheckIsExists( By by) {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(Exception e) {LogHelper.put(e);returnfalse; }}

}}

关于日志帮助类,代码如下:

备注

行路难·其一

【作者】李白 【朝代】唐

金樽清酒斗十千,玉盘珍羞直万钱。

停杯投箸不能食,拔剑四顾心茫然。

欲渡黄河冰塞川,将登太行雪满山。

闲来垂钓碧溪上,忽复乘舟梦日边。

行路难,行路难,多歧路,今安在?

长风破浪会有时,直挂云帆济沧海。

作者:Alan.hsiang

出处:http://www.cnblogs.com/hsiang/

版权声明:本文来源于网友收集或网友供稿,仅供学习交流之用,如果有侵权,请转告小编或者留言,本公众号立即删除。

支持小薇

福利 :

关注公众号: DotNet开发跳槽 ❀

觉得不错,请点个在看 呀

扫描二维码推送至手机访问。

版权声明:本文由飞速云SEO网络优化推广发布,如需转载请注明出处。

本文链接:http://zspsrg.cn/post/117437.html

分享给朋友:

“点击确定按钮跳转网页的代码(点击确定后将跳转至最新版下载页面)” 的相关文章

常德软件开发(常德软件开发中职学校)

常德软件开发(常德软件开发中职学校)

本篇文章给大家谈谈常德软件开发,以及常德软件开发中职学校对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。 本文目录一览: 1、常德职业技术学院专业软件开发专业好赚钱吗 2、常德市沅梦网络科技有限公司怎么样? 3、常德市乐学软件开发有限公司怎么样? 常德职业技术学院专业软件开发专业好赚...

十大软件公司排行榜(全国前十名的软件公司)

十大软件公司排行榜(全国前十名的软件公司)

今天给各位分享十大软件公司排行榜的知识,其中也会对全国前十名的软件公司进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!本文目录一览: 1、中国十大软件公司是哪些公司??中国十大软件是哪些软件? 2、中国saas软件十大排名 3、erp软件排名 4、软件开发十大排行榜...

使命召唤手游极品账号图片(使命召唤极品账号密码)

使命召唤手游极品账号图片(使命召唤极品账号密码)

今天给各位分享使命召唤手游极品账号图片的知识,其中也会对使命召唤极品账号密码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!本文目录一览: 1、使命召唤手游账号密码 2、使命召唤战区手游账号注册 3、使命召唤战区手游账号有哪些 使命召唤手游账号密码 使命召唤手游账号密...

每天能赚30—50元的游戏不用看广告(一分钟赚50元的游戏不用看广告)

每天能赚30—50元的游戏不用看广告(一分钟赚50元的游戏不用看广告)

今天给各位分享每天能赚30—50元的游戏不用看广告的知识,其中也会对一分钟赚50元的游戏不用看广告进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!本文目录一览: 1、真正能赚钱的游戏无广告 2、什么游戏一天赚30元的,求能赚人民币的网络游戏,最好能一天赚30元的 3、一...

微信商家小程序怎么申请视频(怎么样申请微信小程序商家)

微信商家小程序怎么申请视频(怎么样申请微信小程序商家)

今天给各位分享微信商家小程序怎么申请视频的知识,其中也会对怎么样申请微信小程序商家进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!本文目录一览: 1、微信小程序怎么申请注册? 2、小程序直播怎么申请开通? 3、微信小程序如何申请 微信小程序申请方法 4、怎样注册微信...

中台架构与实现(中台架构与实现基于DDD和微服务)

中台架构与实现(中台架构与实现基于DDD和微服务)

今天给各位分享中台架构与实现的知识,其中也会对中台架构与实现基于DDD和微服务进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!本文目录一览: 1、中台是什么意思 2、数据中台有什么好处? 3、业务中台和数据中台有什么关系? 4、百胜软件E3全渠道中台采用的什么底层架...