Setting screen size in PhantomJS C# being driven b

2019-01-26 17:51发布

How do I set the browser screen size? When I take a screen shot after setting up PhantomJS as below its only showing a 400px wide image.

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

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

1条回答
Ridiculous、
2楼-- · 2019-01-26 18:17

It should be the same as setting window size in any other browsers, feel free to have a look at this example: How to get window size, resize or maximize window using Selenium WebDriver.

Here is the code tested working for me:

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);
}
查看更多
登录 后发表回答