C#使用selenium實現(xiàn)操作瀏覽器并且截圖
更新時間:2024年01月04日 15:39:21 作者:代碼寫到35歲
這篇文章主要為大家詳細介紹了C#如何使用selenium組件實現(xiàn)操作瀏覽器并且截圖,文中的示例代碼簡潔易懂,有需要的小伙伴可以參考一下
1.背景
需要完成一個統(tǒng)計報表的定時推送功能,所有定時打開統(tǒng)計報表界面進行截圖。在網(wǎng)上找了一圈,最后決定使用selenium組件進行操作瀏覽器。
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();
//設置查找元素最大超時時間為30秒
timeouts.ImplicitWait = new TimeSpan(0, 0, 30);
//設置頁面操作最大超時時間為30秒
timeouts.PageLoad = new TimeSpan(0, 0, 30);
//設置腳本異步最大超時時間為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)
到此這篇關于C#使用selenium實現(xiàn)操作瀏覽器并且截圖的文章就介紹到這了,更多相關C# selenium操作瀏覽器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用C# CefSharp Python采集某網(wǎng)站簡歷并且自動發(fā)送邀請短信的方法
這篇文章主要給大家介紹了關于如何使用C# CefSharp Python采集某網(wǎng)站簡歷并且自動發(fā)送邀請短信的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧2019-03-03
基于C#實現(xiàn)任意格式JSON文本的HTTP交互抽象類
為了實現(xiàn)一個支持任意格式JSON交互的抽象類,并且在整個過程中不需要對JSON格式數(shù)據(jù)進行序列化和反序列化操作,可以使用C#中的HttpClient類來進行HTTP請求和響應,本文給大家介紹了基于C#實現(xiàn)任意格式JSON文本的HTTP交互抽象類,需要的朋友可以參考下2025-03-03
C#使用WebSocket與網(wǎng)頁實時通信的實現(xiàn)示例
本文主要介紹了C#使用WebSocket與網(wǎng)頁實時通信的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08

