我試圖捕獲整個瀏覽器屏幕(例如任何工具欄,面板等)的屏幕截圖,不僅整個頁面,所以我得到了這個代碼:從Selenium webdriver的當前窗口獲取窗口句柄(IntPtr)GUID
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");
}
好的技巧,順便說一句,但如果有兩個不同具有相同標題的FireFox實例? – kseen 2012-07-20 10:47:22
@kseen您可以用['SingleOrDefault']替換['FirstOrDefault'](http://msdn.microsoft.com/zh-cn/library/bb549039)(http://msdn.microsoft.com/zh-cn//library/bb549274),這樣如果有兩個具有相同標題的瀏覽器實例,則會得到一個異常。如果你在測試案例中,你可以斷言它。 – 2012-07-20 11:29:30
@PaoloMoretti我怎樣才能得到當前窗口(手動打開的現有窗口),這是不是由驅動程序打開意味着我無法使用driver.getWindowHandle(); – 2015-10-10 09:21:46