In a multi-monitor display environment, how do I tell Selenium which display to open a new window in?

Viewed 29964

Sorry if this has been asked and answered. I did a search but came up empty.

19 Answers

There's actually a fairly easy way to do this. There's a method called 'set_window_position' which accepts negative values. So I wanted the browser to open on my left screen, so a simple negative 1000px pixels dragged it in enough for the maximize_window to pick the left screen.

driver.set_window_position(-1000, 0)
driver.maximize_window()

So depending on the screen sizes and where you want it to go, make some calculations and just drag it there!

Source: http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.firefox.webdriver (picked firefox for this example)

Wailord's answer worked for me but it always opened the window and then moved it. So there is a brief moment where it blocks my editor.

Chrome has a command-line switch for window position
--window-position=x,y
https://peter.sh/experiments/chromium-command-line-switches/#window-position

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('window-position=2000,0')  # opens at x=2000,y=0 from the start
driver = webdriver.Chrome(options=options)

use this condiction:

WebDriver chrome1 = new ChromeDriver();
WebDriver chrome2 = new ChromeDriver();

chrome1.manage().window().maximize();//display 1

chrome2.manage().window().setPosition(new Point(2000,0));//display 2
Thread.sleep(1000);// 1 sec
chrome2.manage().window().maximize();//maximize display 2

On Mac you can control this in System Preferences > Displays > Arrangement

There is a white bar at the top of the primary screen. Drag it to the screen where you want the browsers to be rendered

In case the monitor where you want to open a browser windows is to the left of the monitor with IDE, try negative values. In Java:

WebDriver driver = new FirefoxDriver();
driver.manage().window().setPosition(new Point(-1500, 0));

Just put the Mac "Menu Bar" on the monitor where you want the browser to open up. That sets the default monitor.

I found a workaround for this which in my case worked for me in Windows 10. I just simply made my second monitor as the Main Display via Display Settings:

Main Display via Display Settings

Hope this helps you as well!

I'm using this in Java, and it's working

public void maximizeToNextMonitor(WebDriver driver, String server) {
    driver.get(server);
    Window window = driver.manage().window();
    window.maximize();
    window.setPosition(new org.openqa.selenium.Point(window.getSize().getWidth(), 0));
    window.maximize();
}
Related