【C# 使用selenium组件操作浏览器并且截图】

1.背景

需要完成一个统计报表的定时推送功能,所有定时打开统计报表界面进行截图。在网上找了一圈,最后决定使用selenium组件进行操作浏览器。

2.代码

csharp 复制代码
 
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官网

https://www.selenium.dev/

相关推荐
覆东流1 分钟前
第7天:Python小项目
开发语言·后端·python
qq_2546177714 分钟前
attribute((constructor)) 在C/C++中的应用
开发语言·c++
xyq202417 分钟前
HTML5 Input 类型详解
开发语言
云深麋鹿18 分钟前
C++ | 多态
开发语言·c++
我是无敌小恐龙22 分钟前
Java SE 零基础入门 Day05 类与对象核心详解(封装+构造方法+内存+变量)
java·开发语言·人工智能·python·机器学习·计算机视觉·数据挖掘
逻辑驱动的ken28 分钟前
Java高频面试考点14
开发语言·数据库·算法·哈希算法
小灰灰搞电子30 分钟前
Python self关键字详解及其应用
开发语言·python
故事还在继续吗31 分钟前
C++17关键特性
开发语言·c++·算法
84869811935 分钟前
Cursor 用 Java + Vue3 做了一个可落地的酒店管理系统(HMS),支持多门店、RBAC、财务结算,源码开源!
java·开发语言·开源
格林威37 分钟前
面阵相机 vs 线阵相机:堡盟与Basler选型差异全解析 +C# 实战演示
开发语言·人工智能·数码相机·计算机视觉·c#·视觉检测·工业相机