Design

Design
asp.net mvc

2018年10月30日 星期二

在winform內鑲Selenium控制

最近研究出可以在winform內鑲瀏覽器用Selenium控制的方法,這樣就不用每次導出視窗 造成一些問題 也比較美觀,主要方法是透過CefSharp內核把chrome鑲入winform,ChromiumWebBrowser的RemoteDebuggingPort建立個本個遠端路徑,再透過 Selenium ChromeOptions的DebuggerAddress 去做畫面的顯示

程式碼如下
 public partial class Form1 : Form
    {
        private readonly ChromiumWebBrowser browser;
        private static readonly bool DebuggingSubProcess = Debugger.IsAttached;

        private ISelenium selenium;
        private StringBuilder verificationErrors;

        public Form1()
        {
            InitializeComponent();

            var settings = new CefSettings();
            settings.RemoteDebuggingPort = 8088;
            settings.IgnoreCertificateErrors = true;
            Cef.Initialize(settings);
            browser = new ChromiumWebBrowser(url)
            {
                Dock = DockStyle.Fill,
            };
            browser.CreateControl();
            this.Controls.Add(browser);
        }

        public static void GO()
        {
            var driverService = ChromeDriverService.CreateDefaultService();
            driverService.HideCommandPromptWindow = true;

            ChromeOptions options = new ChromeOptions();
            options.DebuggerAddress = "127.0.0.1:8088";
            options.AddArguments("--enable-logging");
            using (var driver = new ChromeDriver(driverService, options))
            {
               //doSomething
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GO();
        }
    }

沒有留言:

張貼留言