Setting Device Width for Remote Webdriver in Selenium Setting Device Width for Remote Webdriver in Selenium selenium selenium

Setting Device Width for Remote Webdriver in Selenium


I don't know how to set the device width using selenium, but I figured out how to set it using the remote frame buffer. For me this was Xvbf. I pass in the screen resolution when I start the service.

Below is an example of an Xvfb service with a resolution of 1024x768 with a depth of 24.https://gist.github.com/dloman/931d65cbb79b00593ac3dd5d0cdf37d9


My experience is limited to using the Python implementation of Selenium so this may not work for you, but there you can use driver.set_window_size(width, height) before performing the driver.get() action that will load the desired page.

The key is the order in which you perform these actions: you want to tell selenium to change the dimensions of the browser before you load the page, because otherwise you are simulating the same behaviour as resizing a browser window after the initial page load. As you may know, when resizing a browser window, a page reload is needed for some 'responsive' features to be activated.