C# 自动化操作 chrome上传文件

该代码示例展示了如何利用CefSharp库自定义文件选择处理器,直接指定文件路径,然后通过JavaScript执行元素定位和鼠标事件模拟,实现自动化点击上传文件的功能。
摘要由CSDN通过智能技术生成

1.首先要设置文件选择处理器

using CefSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test.browser
{
    class ChromiumSZDialogHandler : IDialogHandler
    {

        private string filePath = "";


        public ChromiumSZDialogHandler(string selectFilePatH)
        {
            this.filePath = selectFilePatH;
        }

        //弹出选择文件框时,直接返回对应的文件路径!!
        public bool OnFileDialog(IWebBrowser chromiumWebBrowser, IBrowser browser, CefFileDialogMode mode, string title, string defaultFilePath, List<string> acceptFilters, IFileDialogCallback callback)
        {
            callback.Continue(new List<string> { this.filePath });
            return true;
        }
    }
}

2.上传文件功能

 // 上传文件功能
        public static void uploadFile(ChromiumWebBrowser browser, string fileFullPath)
        {
            //设置文件选择处理器
            browser.DialogHandler = new ChromiumSZDialogHandler(fileFullPath);

            IFrame iframe2 = ChromiumUtils.getIFrame(browser, 0);

            //先将元素滚动到能看到的位置
           ExecuteJavaScriptAsync(iframe2, "document.getElementsByClassName('el-button el-button--primary el-button--medium')[3].scrollIntoView();");

            //定位元素的坐标,然后点击鼠标
            string x = excuteJSstr(iframe2, "document.getElementsByClassName('el-button el-button--primary el-button--medium')[3].getBoundingClientRect().x");
            string y = excuteJSstr(iframe2, "document.getElementsByClassName('el-button el-button--primary el-button--medium')[3].getBoundingClientRect().y");
            string width = excuteJSstr(iframe2, "document.getElementsByClassName('el-button el-button--primary el-button--medium')[3].getBoundingClientRect().width");
            string height = excuteJSstr(iframe2, "document.getElementsByClassName('el-button el-button--primary el-button--medium')[3].getBoundingClientRect().height");
            LogUtils.info("x = " + x + " | y = " + y + "| width = " + width + " | height = " + height);
            double xx = double.Parse(x) + double.Parse(width) / 2;
            double yy = double.Parse(y) + double.Parse(height) / 2;

            // 点击
            SendMouseClickEvent(iframe2.Browser.GetHost(), ((int)xx), ((int)yy), MouseButtonType.Left, false, 1, CefEventFlags.None);
            // 松开
            SendMouseClickEvent(iframe2.Browser.GetHost(), ((int)xx), ((int)yy), MouseButtonType.Left, true, 1, CefEventFlags.None);
        }

  public static void SendMouseClickEvent(this IBrowserHost host, int x, int y, MouseButtonType mouseButtonType, bool mouseUp, int clickCount, CefEventFlags modifiers)
        {
            ThrowExceptionIfBrowserHostNull(host);

            host.SendMouseClickEvent(new MouseEvent(x, y, modifiers), mouseButtonType, mouseUp, clickCount);
        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值