小编典典

在由Selenium驱动的PhantomJS C#中设置屏幕大小

selenium

如何设置浏览器的屏幕尺寸?在设置PhantomJS之后,当我拍摄如下屏幕快照时,它仅显示400px宽的图像。

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

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

阅读 560

收藏
2020-06-26

共1个答案

小编典典

它应该与在其他任何浏览器中设置窗口大小相同,请看以下示例:如何使用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);
}
2020-06-26