C#使用selenium實(shí)現(xiàn)操作瀏覽器并且截圖
1.背景
需要完成一個(gè)統(tǒng)計(jì)報(bào)表的定時(shí)推送功能,所有定時(shí)打開統(tǒng)計(jì)報(bào)表界面進(jìn)行截圖。在網(wǎng)上找了一圈,最后決定使用selenium組件進(jìn)行操作瀏覽器。
2.代碼
using OpenQA.Selenium.Edge; using OpenQA.Selenium; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing.Imaging; using System.Threading; using OpenQA.Selenium.IE; using OpenQA.Selenium.Remote; using OpenQA.Selenium.Support.Extensions; using System.Xml.Linq; using System.IO; namespace ScreenCutIamge { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.formLoad(); this.timer1.Start(); } public void formLoad() { this.txt_document.Text = "pageContentDIV"; this.txt_path.Text = "D:\\cutimage"; this.txt_scal.Text = "40"; this.txt_url.Text = "https://www.baidu.com"; } private void button1_Click(object sender, EventArgs e) { this.cutImage(); } private void cutImage() { var service = EdgeDriverService.CreateDefaultService(@".", "msedgedriver.exe"); using (IWebDriver driver = new OpenQA.Selenium.Edge.EdgeDriver(service)) { driver.Navigate().GoToUrl(this.txt_url.Text); //driver.Url = "http://www.baidu.com"是一樣的 //var options = new InternetExplorerOptions(); // string script = "document.body.style.transform='scale(0.5)'"; // driver.ExecuteJavaScript(script); Thread.Sleep(10000); string script = "document.getElementsByClassName('" + this.txt_document.Text + "')[0].style.zoom = '" + this.txt_scal.Text + "%'"; driver.ExecuteJavaScript(script); driver.Manage().Window.Maximize(); driver.Manage().Window.FullScreen(); //options.IgnoreZoomLevel = true; // options. // var driver = new RemoteWebDriver(options); ITimeouts timeouts = driver.Manage().Timeouts(); //設(shè)置查找元素最大超時(shí)時(shí)間為30秒 timeouts.ImplicitWait = new TimeSpan(0, 0, 30); //設(shè)置頁(yè)面操作最大超時(shí)時(shí)間為30秒 timeouts.PageLoad = new TimeSpan(0, 0, 30); //設(shè)置腳本異步最大超時(shí)時(shí)間為30秒 timeouts.AsynchronousJavaScript = new TimeSpan(0, 0, 30); var source = driver.PageSource; // this.txt_scal.Text = source; Screenshot screenShotFile = ((ITakesScreenshot)driver).GetScreenshot(); string imageName = "test" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png"; if (!Directory.Exists(txt_path.Text)) { Directory.CreateDirectory(txt_path.Text); } imageName = txt_path.Text + "\\" + imageName; screenShotFile.SaveAsFile(imageName); } } private void timer1_Tick(object sender, EventArgs e) { cutImage(); } } }
3.selenium官網(wǎng)
到此這篇關(guān)于C#使用selenium實(shí)現(xiàn)操作瀏覽器并且截圖的文章就介紹到這了,更多相關(guān)C# selenium操作瀏覽器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用C# CefSharp Python采集某網(wǎng)站簡(jiǎn)歷并且自動(dòng)發(fā)送邀請(qǐng)短信的方法
這篇文章主要給大家介紹了關(guān)于如何使用C# CefSharp Python采集某網(wǎng)站簡(jiǎn)歷并且自動(dòng)發(fā)送邀請(qǐng)短信的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2019-03-03基于C#實(shí)現(xiàn)任意格式JSON文本的HTTP交互抽象類
為了實(shí)現(xiàn)一個(gè)支持任意格式JSON交互的抽象類,并且在整個(gè)過(guò)程中不需要對(duì)JSON格式數(shù)據(jù)進(jìn)行序列化和反序列化操作,可以使用C#中的HttpClient類來(lái)進(jìn)行HTTP請(qǐng)求和響應(yīng),本文給大家介紹了基于C#實(shí)現(xiàn)任意格式JSON文本的HTTP交互抽象類,需要的朋友可以參考下2025-03-03Unity3D使用陀螺儀控制節(jié)點(diǎn)旋轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Unity3D使用陀螺儀控制節(jié)點(diǎn)旋轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11C#使用WebSocket與網(wǎng)頁(yè)實(shí)時(shí)通信的實(shí)現(xiàn)示例
本文主要介紹了C#使用WebSocket與網(wǎng)頁(yè)實(shí)時(shí)通信的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08