How to set window-size to fullscreen for headless-chrome using chrome options?

Deadly 提交于 2021-02-04 14:58:09

问题


When executing UI tests, I get an error that selenium doesn't support automatic window resizing for chromedriver, which results in tests failing.

Is there a way to set this using chrome-options for headless-chrome ?

I have tried the following,

ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");

Also, replacing "--start-maximized" with "--start-fullscreen" and "--kiosk".

But none of the above worked for me, the only option that works for me is "--window-size=width,height".

I do not want to hard-code values for width and height, is there some way I can do this to set fullscreen ?


回答1:


The problem is that headless mode is meant to be used on computers without screens, so there's no way for it to figure out what size your screen is even if you have one. The only way is for you to pass that information to the browser with --window-size.

The default window size and display size in headless mode is 800x600 on all platforms.

So the maximized window size is not applicable for chrome-headless and needs to be explicitly set by users, if required.




回答2:


this worked for me driver.manage().window().setSize(new Dimension(1600,700));




回答3:


There is another option. Instead of --start-maximized option, you can do:

WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();


来源:https://stackoverflow.com/questions/44796194/how-to-set-window-size-to-fullscreen-for-headless-chrome-using-chrome-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!