简体   繁体   中英

Selenium can't handle multiple ChromiumWebBrowser instances in C#

I have two instances of the ChromiumWebBrowser in my WinForms project (Visual Studio 2012). My goal is to have the second browser instance "copy" the behavior of the user input in the first browser instance. I can successfully retrieve the input from the first browser, and I managed to hook up Selenium in the project as well.

However, I'm having one issue. Whenever Selenium sends its commands, the first browser is the one that responds to them. For the life of me, I can't seem to figure out how to make the second browser respond. Whenever I completely remove the first browser, the second one starts responding correctly, but adding the first browser again will make only have the first browser use the Selenium commands. I even tried to switch out the moments the browsers are added to the form, but to no avail: whenever there are two available, the wrong one is responsive.

Relevant code:

    public BrowserManager(Controller controller, string startingUrl)
    {
        _controller = controller;

        var settings = new CefSettings { RemoteDebuggingPort = 9515 };
        Cef.Initialize(settings);

        // Input browser
        inputBrowser = new ChromiumWebBrowser(startingUrl);
        var obj = new XPathHelper(this);
        inputBrowser.RegisterJsObject("bound", obj); //Standard object registration
        inputBrowser.FrameLoadEnd += obj.OnFrameLoadEnd;


        // Output browser
        var browserSettings = new BrowserSettings();
        var requestContextSettings = new RequestContextSettings { CachePath = "" };
        var requestContext = new RequestContext(requestContextSettings);
        outputBrowser = new ChromiumWebBrowser(startingUrl);
        outputBrowser.RequestContext = requestContext;
        outputBrowser.AddressChanged += InitializeOutputBrowser;
        outputBrowser.Enabled = false;
        outputBrowser.Name = "outputBrowser";
    }

The selenium part:

public class SeleniumHelper
{
    public SeleniumHelper()
    {
        DoWorkAsync();
    }

    private Task DoWorkAsync()
    {
        Task.Run(() =>
        {
            string chromeDriverDir = @"ActionRecorder\bin\x64\Debug\Drivers";
            var chromeDriverService = ChromeDriverService.CreateDefaultService(chromeDriverDir);
            chromeDriverService.HideCommandPromptWindow = true;

            ChromeOptions options = new ChromeOptions();
            options.BinaryLocation = @"ActionRecorder\bin\x64\Debug\ActionRecorder.exe";
            options.DebuggerAddress = "127.0.0.1:9515";
            options.AddArguments("--enable-logging");

            using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(chromeDriverService, options))
            {
                driver.Navigate().GoToUrl("http://www.google.com");

                var query = driver.FindElement(By.Name("q"));

                query.SendKeys("A google search test");

                query.Submit();
            }
        });

        return null;
    }
}

And finally, a screenshot for some visualization:

在此处输入图片说明

Some help with the issue would be very much appreciated. If i missed some crucial info, feel free to ask for it. Thanks in advance!

Greetz, Tybs

The behavior is correct. You have one debug address and you can only have one debug address for CEF. Which means when you use Selenium it is only seeing one browser.

By default Selenium will send an command to current active Tab or Window. Now in your case you have multiple Chrome view embedded, but they are technically Chrome Tab/Windows which you have placed on the same form.

So if you are in luck below code in should be able to move you to the Window you are interested in

driver.SwitchTo().Window(driver.WindowHandles.Last());

See if it works. If it doesn't then your only other workaround would be to change the order of Adding ChromiumWebBrowser and that should reverse the window it works on.

Below are some important threads that you should read from top to bottom. Very relevant to your issue/request

https://code.google.com/archive/p/chromiumembedded/issues/421 https://github.com/cefsharp/CefSharp/issues/1076

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM