最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#使用selenium實(shí)現(xiàn)操作瀏覽器并且截圖

 更新時(shí)間:2024年01月04日 15:39:21   作者:代碼寫到35歲  
這篇文章主要為大家詳細(xì)介紹了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è)置頁面操作最大超時(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)

https://www.selenium.dev/

到此這篇關(guān)于C#使用selenium實(shí)現(xiàn)操作瀏覽器并且截圖的文章就介紹到這了,更多相關(guān)C# selenium操作瀏覽器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

庆云县| 广水市| 靖边县| 鱼台县| 大城县| 敦煌市| 运城市| 象州县| 宾阳县| 古蔺县| 新密市| 太仓市| 库车县| 东阿县| 芒康县| 平舆县| 土默特左旗| 连城县| 综艺| 潼关县| 麻城市| 遵化市| 枣庄市| 水富县| 榆树市| 随州市| 泽州县| 阜新市| 新密市| 建瓯市| 紫金县| 搜索| 武乡县| 阿克苏市| 个旧市| 郁南县| 漯河市| 时尚| 静海县| 迁安市| 成都市|