自拍偷在线精品自拍偷|国产无码一区二区久久|最新版天堂资源中文官网|国产精品第一页爽爽影院|国产精品一区二区av不卡|久久久波多野av一区无码|国产欧美日本亚洲精品一4区|亚洲精品天堂在线观看2020

當前位置:首頁 > 網(wǎng)站建設 > 正文內(nèi)容

htmlspanurl(htmlspan標簽)

網(wǎng)站建設10個月前 (04-04)387

概述

Selenium是一款免費的分布式的自動化測試工具,支持多種開發(fā)語言,無論是C、 java、ruby、python、或是C# ,你都可以通過selenium完成自動化測試。本文以一個簡單的小例子,簡述C# 利用Selenium進行瀏覽器的模擬操作,僅供學習分享使用,如有不足之處,還請指正。

涉及知識點

要實現(xiàn)本例的功能,除了要掌握Html ,Java,CSS等基礎(chǔ)知識,還涉及以下知識點:

log4net:主要用于日志的記錄和存儲,本例采用log4net進行日志記錄,便于過程跟蹤和問題排查,關(guān)于log4net的配置和介紹,之前已有說明,本文不做贅述。

Queue:隊列,先進先出模式,本文主要用于將日志信息保存于隊列中,然后再顯示到頁面上,其中Enqueue用于添加內(nèi)容到結(jié)尾處,Dequeue用于返回并移除一個位置的對象。

IWebDriver:瀏覽器驅(qū)動接口,所有的關(guān)于瀏覽器的操作都可以通過此接口進行,不同瀏覽器有不同的實現(xiàn)類,如:IE瀏覽器(InternetExplorerDriver)Chrome瀏覽器(ChromeDriver)等。

BackgroundWorker:后臺工作線程,區(qū)別于主線程,通過事件觸發(fā)不同的狀態(tài)。

本例開發(fā)工具為VS2019,通過NuGet進行需要的軟件包的安裝與管理,如下所示:

示例效果圖

本例采用Chrome瀏覽器,用于監(jiān)控某一個網(wǎng)站并獲取相應內(nèi)容,如下所示:

展開全文

Selenium示例介紹

定義一個webDriver,如下所示:

通過ID獲取元素并填充內(nèi)容和觸發(fā)事件,如下所示:

通過XPath獲取元素,如下所示:

核心代碼

主要的核心代碼,就是瀏覽器的元素定位查找和事件觸發(fā),如下所示:

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

///summary///驅(qū)動 ////summaryprivateIWebDriver driver = null;

///summary///# 無貨 ////summaryprivatestringno_stock = "Currently Out of Stock";

///summary///# 線程等待秒數(shù) ////summaryprivateintwait_sec = 2;

privateDictionary string, string cfg_info;

privatestringwork_path = string.Empty;

///summary///構(gòu)造函數(shù) ////summarypublicSmoking( ) {

}

///summary///帶參構(gòu)造函數(shù) ////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. 定義瀏覽器驅(qū)動對象this.driver = newInternetExplorerDriver(options); }}this.run(url, username, password, item_id); }catch(Exception e) {LogHelper.put( "運行過程中出錯,請重新打開再試"+e.StackTrace); }}

htmlspanurl(htmlspan標簽)

///summary///運行 ////summary///param name="url"/param///param name="username"/param///param name="password"/param///param name="item_id"/paramprivatevoidrun( stringurl, stringusername, stringpassword, stringitem_id ) {//"""運行起來"""http://# 3. 訪問網(wǎng)站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; //# 如果驅(qū)動不為空,則關(guān)閉//self.close_browser_nicely(self.__driver)if( this.driver != null) {this.driver.Quit; //# 關(guān)閉后切要為None,否則啟動報錯this.driver = null; }}catch(Exception e) {//print('Stop Failure')}finally{this.driver = null; }}

privatevoidlogin( stringusername, stringpassword ) {//# 5. 點擊鏈接跳轉(zhuǎn)到登錄頁面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///工作狀態(tài) ////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 = "http://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 = "http://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 = "http://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; }}

}}

關(guān)于日志幫助類,代碼如下:

備注

行路難·其一

【作者】李白 【朝代】唐

金樽清酒斗十千,玉盤珍羞直萬錢。

停杯投箸不能食,拔劍四顧心茫然。

欲渡黃河冰塞川,將登太行雪滿山。

閑來垂釣碧溪上,忽復乘舟夢日邊。

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

長風破浪會有時,直掛云帆濟滄海。

作者:Alan.hsiang

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

版權(quán)聲明:本文來源于網(wǎng)友收集或網(wǎng)友供稿,僅供學習交流之用,如果有侵權(quán),請轉(zhuǎn)告小編或者留言,本公眾號立即刪除。

支持小薇

福利 :

關(guān)注公眾號: DotNet開發(fā)跳槽 ?

覺得不錯,請點個在看 呀

掃描二維碼推送至手機訪問。

版權(quán)聲明:本文由飛速云SEO網(wǎng)絡優(yōu)化推廣發(fā)布,如需轉(zhuǎn)載請注明出處。

本文鏈接:http://www.thonggone.com/post/102638.html

標簽: htmlspanurl

“htmlspanurl(htmlspan標簽)” 的相關(guān)文章

關(guān)于深圳網(wǎng)站建設公司的信息

關(guān)于深圳網(wǎng)站建設公司的信息

今天給各位分享深圳網(wǎng)站建設公司的知識,其中也會對進行解釋,如果能碰巧解決你現(xiàn)在面臨的問題,別忘了關(guān)注本站,現(xiàn)在開始吧!本文目錄一覽: 1、深圳網(wǎng)站建設哪家公司好,深圳最好的建站公司 2、深圳哪個網(wǎng)站建設公司好? 3、深圳網(wǎng)站設計公司哪家比較好? 4、網(wǎng)站建設公司哪家比較好 5、深圳高...

免費網(wǎng)站seo診斷(網(wǎng)站seo查詢技術(shù))

免費網(wǎng)站seo診斷(網(wǎng)站seo查詢技術(shù))

今天給各位分享免費網(wǎng)站seo診斷的知識,其中也會對網(wǎng)站seo查詢技術(shù)進行解釋,如果能碰巧解決你現(xiàn)在面臨的問題,別忘了關(guān)注本站,現(xiàn)在開始吧!本文目錄一覽: 1、網(wǎng)站seo要診斷哪些內(nèi)容呢 2、seo免費工具有哪些 3、seo免費診斷工具有哪些 網(wǎng)站seo要診斷哪些內(nèi)容呢 網(wǎng)站seo要診斷哪些...

優(yōu)化一個網(wǎng)站(優(yōu)化網(wǎng)站是什么)

優(yōu)化一個網(wǎng)站(優(yōu)化網(wǎng)站是什么)

今天給各位分享優(yōu)化一個網(wǎng)站的知識,其中也會對優(yōu)化網(wǎng)站是什么進行解釋,如果能碰巧解決你現(xiàn)在面臨的問題,別忘了關(guān)注本站,現(xiàn)在開始吧!本文目錄一覽: 1、網(wǎng)站優(yōu)化的方法有哪些? 2、網(wǎng)站怎么優(yōu)化? 3、怎樣優(yōu)化網(wǎng)站 如何進行網(wǎng)站優(yōu)化 4、網(wǎng)站優(yōu)化有哪些方法? 網(wǎng)站優(yōu)化的方法有哪些? 1.網(wǎng)站...

網(wǎng)站建設方案書(騰訊云網(wǎng)站建設方案書)

網(wǎng)站建設方案書(騰訊云網(wǎng)站建設方案書)

本篇文章給大家談談網(wǎng)站建設方案書,以及騰訊云網(wǎng)站建設方案書對應的知識點,希望對各位有所幫助,不要忘了收藏本站喔。 本文目錄一覽: 1、網(wǎng)站建設方案書 2、網(wǎng)站建設規(guī)劃書 3、怎么寫網(wǎng)站建設方案(網(wǎng)站策劃書) 4、阿里云備案網(wǎng)站建設方案書 可要可不要么 5、網(wǎng)站建設項目策劃書 網(wǎng)站建...

活動流程ppt模板(活動流程模板 活動策劃)

活動流程ppt模板(活動流程模板 活動策劃)

今天給各位分享活動流程ppt模板的知識,其中也會對活動流程模板 活動策劃進行解釋,如果能碰巧解決你現(xiàn)在面臨的問題,別忘了關(guān)注本站,現(xiàn)在開始吧!本文目錄一覽: 1、有哪些PPT模板免費下載網(wǎng)站? 2、潑水節(jié)活動策劃案 3、公司年會要做PPT,有沒有免費的PPT模板網(wǎng)站推薦一下,謝謝! 4、...

招聘服務員信息模板最新(招聘服務員信息模板文字)

招聘服務員信息模板最新(招聘服務員信息模板文字)

本篇文章給大家談談招聘服務員信息模板最新,以及招聘服務員信息模板文字對應的知識點,希望對各位有所幫助,不要忘了收藏本站喔。 本文目錄一覽: 1、星瑞美麗華酒店招聘在幾樓 2、服務員辭職信怎么寫? 3、銀行餐廳當服務員好嗎 4、酒吧一直高薪招男女服務員是真的嗎? 5、網(wǎng)上招聘說自己不想...