【问题标题】:Need to get the Process id of IEDriverServer.exe so that I can fetch the child PIDs for browser需要获取 IEDriverServer.exe 的进程 ID,以便我可以获取浏览器的子 PID
【发布时间】:2020-04-30 10:13:23
【问题描述】:

问题是我需要获取 IE 浏览器实例的 PID,以便我可以关闭 IE 浏览器(在 C# 中工作)。 我使用 Selenium 启动了 IE 浏览器,然后将 Driver Service 类用作:-

InternetExplorerDriverService driverdetails = InternetExplorerDriverService.CreateDefaultService();
        Console.WriteLine(driverdetails.Port);

计划是获取端口,然后拥有它的子进程。我可以通过手动输入 Port 的值来使用调试器来做到这一点。但是,driverdetails.Port 获取的端口不是我的驱动程序使用的实际端口。

有没有,我可以找到任何给定驱动程序服务的端口?

对于 IE,我有一个替代方法来启动 IE 并获取带有 http://localhost: 端口的 URL。但是,其他浏览器并非如此。我想要制作通用代码,因此我使用的是 Driver Service 对象。

【问题讨论】:

    标签: c# internet-explorer selenium-webdriver


    【解决方案1】:

    据我所知,InternetExplorerDriverService's ProcessID property获取的是正在运行的驱动服务可执行文件的进程ID,我们无法通过InternetExplorer webdriver获取IE浏览器实例PID。如果你想得到PID,你可以尝试使用Process class

    根据您的描述,您似乎想使用 IE Webdriver 关闭 IE 选项卡或窗口。如果是这样,我建议你可以使用InternetExplorerDriver WindowHandles获取打开的窗口,然后使用switchto方法切换窗口并检查url或标题,最后调用Close方法关闭IE窗户。请参考以下示例代码:

        private const string URL = @"https://dillion132.github.io/login.html";
        private const string IE_DRIVER_PATH = @"D:\Downloads\webdriver\IEDriverServer_x64_3.14.0";  // where the Selenium IE webdriver EXE is.
        static void Main(string[] args)
        { 
            InternetExplorerOptions opts2 = new InternetExplorerOptions() { InitialBrowserUrl = "https://www.bing.com", IntroduceInstabilityByIgnoringProtectedModeSettings = true, IgnoreZoomLevel = true };
            using (var driver = new InternetExplorerDriver(IE_DRIVER_PATH, opts2))
            {
                driver.Navigate(); 
                Thread.Sleep(5000);
                //execute javascript script
                var element = driver.FindElementById("sb_form_q");
                var script = "document.getElementById('sb_form_q').value = 'webdriver'; console.log('webdriver')";
                IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
                jse.ExecuteScript(script, element);
    
    
                InternetExplorerDriverService driverdetails = InternetExplorerDriverService.CreateDefaultService(IE_DRIVER_PATH);
                Console.WriteLine(driverdetails.Port);
    
                // open multiple IE windows using webdriver.
                string url = "https://www.google.com/";
                string javaScript = "window.open('" + url + "','_blank');";
    
                IJavaScriptExecutor jsExecutor = (IJavaScriptExecutor)driver;
                jsExecutor.ExecuteScript(javaScript);
    
                Thread.Sleep(5000);
    
                //get all opened windows (by using IE Webdriver )
                var windowlist = driver.WindowHandles;
                Console.WriteLine(windowlist.Count);
    
                //loop through the list and switchto the window, and then check the url 
                if(windowlist.Count > 1)
                { 
                    foreach (var item in windowlist)
                    {
                        driver.SwitchTo().Window(item);
                        Console.WriteLine(driver.Url);
    
                        if(driver.Url.Contains("https://www.bing.com"))
                        {
                            driver.Close(); //use the Close method to close the window. The Quit method will close the browser window and dispose the webdriver.
    
                        }
    
                    }
                }
    
                Console.ReadKey();
            }
    
            Console.ReadKey();
    
        }
    

    【讨论】:

    • 感谢您的帮助.. 虽然我已经找到了出路.. 我首先使用 DriverService,然后使用 DriverService 初始化 Web 驱动程序。所以它是这样工作的。我的问题是关闭在测试中打开的所有浏览器实例。不幸的是,driver.quit() 每次都不能正常工作。 :(
    • 我仍然没有为 IE 实例获得正确的端口。要关闭所有打开的IE浏览器窗口,可以参考上面的代码,循环处理句柄,调用driver.Close方法关闭窗口。
    猜你喜欢
    • 1970-01-01
    • 2021-08-08
    • 2012-05-31
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    相关资源
    最近更新 更多