gpt4 book ai didi

c# - 从 Selenium webdriver 的当前窗口 GUID 获取窗口句柄 (IntPtr)

转载 作者:可可西里 更新时间:2023-11-01 09:12:00 25 4
gpt4 key购买 nike

我正在 try catch 整个浏览器屏幕(例如任何工具栏、面板等)的屏幕截图,而不仅仅是整个页面,所以我得到了这段代码:

using (FirefoxDriver driver = new FirefoxDriver())
{
driver.Navigate().GoToUrl(url);

ScreenCapture sc = new ScreenCapture();

// How can I find natural IntPtr handle of window here, using GUID-like identifier returning by driver.currentWindowHandle?
Image img = sc.CaptureWindow(...);
MemoryStream ms = new MemoryStream();
img.Save(ms, ImageFormat.Jpeg);
return new FileStreamResult(ms, "image/jpeg");
}

最佳答案

您可以使用 Process.GetProcesses 获取窗口句柄:

using (FirefoxDriver driver = new FirefoxDriver())
{
driver.Navigate().GoToUrl(url);

string title = String.Format("{0} - Mozilla Firefox", driver.Title);
var process = Process.GetProcesses()
.FirstOrDefault(x => x.MainWindowTitle == title);

if (process != null)
{
var screenCapture = new ScreenCapture();
var image = screenCapture.CaptureWindow(process.MainWindowHandle);
// ...
}
}

这当然假设您有一个具有该特定标题的浏览器实例。

关于c# - 从 Selenium webdriver 的当前窗口 GUID 获取窗口句柄 (IntPtr),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11526260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com