问题是我需要获取IE浏览器实例的PID,以便关闭IE浏览器(在C#中工作(。我使用Selenium启动了IE浏览器,然后使用驱动程序服务类作为:-
InternetExplorerDriverService driverdetails = InternetExplorerDriverService.CreateDefaultService();
Console.WriteLine(driverdetails.Port);
计划是获得端口,然后拥有其子进程。我可以使用调试器手动输入Port的值来完成此操作。但是,端口是由驱动程序详细信息获取的。端口不是我的司机实际使用的端口。
有没有,我可以找到任何给定司机服务的港口?
对于IE,我有一个替代方案,可以启动IE并获取带有端口的URLhttp://localhost:.然而,其他浏览器的情况并非如此。我想要的是生成通用代码,因此我使用驱动程序服务对象。
据我所知,InternetExplorerDriverService的ProcessID属性获取正在运行的驱动程序服务可执行文件的进程ID,而我们无法通过InternetExplorer网络驱动程序获取IE浏览器实例PID。如果您想获得PID,可以尝试使用Process类。
根据您的描述,您似乎想使用IE Web驱动程序关闭IE选项卡或窗口。如果是这种情况,我建议您可以使用InternetExplorerDriver WindowHandles来获取打开的窗口,然后使用switchto
方法切换窗口并检查url或标题,最后调用Close
方法关闭IE窗口。请参考以下示例代码:
private const string URL = @"https://dillion132.github.io/login.html";
private const string IE_DRIVER_PATH = @"D:DownloadswebdriverIEDriverServer_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();
}