本文介绍了如何将Selenium WebDriver嵌入为WPF控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以将WebDriver驱动程序嵌入到WPF窗口中,类似于WPF的WebBrowser控件?

Is there a way to embed a WebDriver driver to a WPF window, similarly to the WPF's WebBrowser control?

(可选)是否可以在WebBrowser控件本身上使用Selenium?

Optionally, is there a way to use Selenium on the WebBrowser control itself?

到目前为止,只能创建一个新的WebDriver窗口,与应用程序中的任何其他WPF窗口分开.

So far, it's only possible to create a new WebDriver window, separate from any other WPF window in the application.

推荐答案

这是一个小技巧;)

由于Selenium大多数时候都使用外部应用程序(浏览器),因此没有像将Browser-UI集成到应用程序中那样的本机"解决方案.

As Selenium uses external applications (the browsers) most of the time there is no "native" solution like just integrating the Browser-UI in an app.

但是,有Windows特定的API可以使用WinForms来实现这些功能.

There are, however, Windows-specific APIs to achieve exactly that with WinForms.

private static class UnsafeNativeMethods {
        [DllImport("user32")]
        public static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
}

基本上,这个想法是将外部浏览器包装"在应用程序的用户控件中.

Basically the idea is to "wrap" the external browser within a usercontrol in your app.

    public void AttachDriverService(DriverService service) {
        //get the process started by selenium
        var driverProcess = Process.GetProcessById(service.ProcessId);

        //find the first child-process (should be the browser)
        var browserProcess = driverProcess.GetChildren()
                                .Where(p => p.ProcessName != "conhost")
                                .First();

        _BrowserHandle = browserProcess.MainWindowHandle;

        //set the parent window utilizing Win32-API
        UnsafeNativeMethods.SetParent(_BrowserHandle.Value, this.Handle);
        //handle moving/resizing of your appo to be reflected on browser
        UnsafeNativeMethods.MoveWindow(_BrowserHandle.Value, 0, 0, Width, Height, true);

        this.Resize += (sender, e) => {
            UnsafeNativeMethods.MoveWindow(_BrowserHandle.Value, 0, 0, Width, Height, true);
        };
    }

最后,该扩展用于通过 WMI :

At last the extension used to enumerate child-processes via WMI:

public static IEnumerable<Process> GetChildren(this Process parent) {
    var query = new ManagementObjectSearcher($@"
        SELECT *
        FROM Win32_Process
        WHERE ParentProcessId={parent.Id}");

    return from item in query.Get().OfType<ManagementBaseObject>()
           let childProcessId = (int)(UInt32)item["ProcessId"]
           select Process.GetProcessById(childProcessId);
}

并调用如下方法:

var host = new SeleniumHost();
var service = InternetExplorerDriverService.CreateDefaultService();
var driver = new InternetExplorerDriver(service);
host.AttachDriverService(service);

完成后,这将解决WinForms-Part.要将其集成到WPF中,您需要利用 WindowsFormsHost 以显示WinForms-Control.

When done, this solves the WinForms-Part. To integrate this in WPF you need to leverage WindowsFormsHost to display the WinForms-Control.

查看我最新发布的在GitHub上的仓库以获得进一步参考或直接利用 NuGet-Package .

Check out my fresh published repo on GitHub for further reference or directly leverage the NuGet-Package.

请多多包涵,因为这些内容非常热门-将来肯定会有错误和进一步的改进(例如从浏览器中删除chrome/border).希望您能得到这个主意和/或在GitHub上做出贡献.

Please bear with me, as those are very hot bits - so there sure will be bugs and further improvements to make in the future (like removing the chrome/border from the browser). Hopefully you can get the idea and/or maybe contribute on GitHub.

这篇关于如何将Selenium WebDriver嵌入为WPF控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 18:50