如何设置浏览器屏幕大小?当我在设置幻影后拍摄屏幕时,它只显示400像素宽的图像。

var driverService = PhantomJSDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
driverService.LoadImages = false;
driverService.ProxyType = "none";

using (var driver = new PhantomJSDriver(driverService))
{
    etc....
}

最佳答案

它应该与在任何其他浏览器中设置窗口大小相同,请随意查看以下示例:How to get window size, resize or maximize window using Selenium WebDriver
下面是为我测试的代码:

var driverService = PhantomJSDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
driverService.LoadImages = false;
driverService.ProxyType = "none";

using (var driver = new PhantomJSDriver(driverService)) {
    driver.Manage().Window.Size = new Size(1920, 1080); // Size is a type in assembly "System.Drawing"
    driver.Url = "http://www.stackoverflow.com";
    driver.TakeScreenshot().SaveAsFile(@"c:\phantomjs_screenshot.png", ImageFormat.Png);
}

09-25 16:19