Mike Jr
Mike Jr

Reputation: 1899

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

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

Upvotes: 54

Views: 41303

Answers (20)

Ghasem Tabatabaei
Ghasem Tabatabaei

Reputation: 165

In my case, this works in C#

IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Position = new System.Drawing.Point(2000, 0); // Adjust X and Y as needed
driver.Manage().Window.Maximize();

Upvotes: 0

Amgad Hanafy
Amgad Hanafy

Reputation: 667

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();
}

Upvotes: 0

Jon Jon Cabangon
Jon Jon Cabangon

Reputation: 11

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!

Upvotes: 0

Rob
Rob

Reputation: 138

In OSX Mavericks you can assign desktop from the Options menu when clicking and holding the icon from the dock. This solved the problem for me.

Upvotes: 9

Nash N
Nash N

Reputation: 359

I have 2 1920x1080 monitors, I move the browser window to the 2nd monitor and maximize it there.

  1. Get screen resolution

    java.awt.Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    double width = screenSize.getWidth();
    double height = screenSize.getHeight();
    
  2. Move browser to second monitor and maximize

    if (width <= 1920) {
        Point point = new Point(width, 0);
        driver.manage().window().setPosition(point);
        driver.manage().window().maximize();
    }
    
  3. If your resolution is wider than a typical monitor, open the browser in a more realistic resolution (this is optional, but I recommend it)

    else 
    
    {
        Point point = new Point(0, 0);
        driver.manage().window().setPosition(point);
    
        Dimension targetWindowSize = new Dimension(1920, 1080);
        driver.manage().window().setSize(targetWindowSize);
    }
    

Upvotes: 4

Kevin McDonnell
Kevin McDonnell

Reputation: 131

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

Upvotes: 1

Ragnoroct
Ragnoroct

Reputation: 175

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)

Upvotes: 8

djangofan
djangofan

Reputation: 29689

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

Upvotes: 0

Igor
Igor

Reputation: 1211

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));

Upvotes: 0

KarlSaik
KarlSaik

Reputation: 21

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

Upvotes: 2

joseph
joseph

Reputation: 1008

I noticed that for me its always opening in the primary monitor. So I changed the primary display from OSX display arrangement options window.

Upvotes: 0

Nick Verheijen
Nick Verheijen

Reputation: 229

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)

Upvotes: 16

cesar celis
cesar celis

Reputation: 21

I have noticed that by setting these things below, my browser gets opened alway in the same monitor (I have two monitors the eDP and a secondary HDMI monitor). Maybe it has to do with the position:

# console log
d = desired_capabilities.DesiredCapabilities.CHROME
d['loggingPrefs'] = { 'browser':'ALL' }

# Driver
self.driver = webdriver.Chrome(desired_capabilities=d)
self.driver.implicitly_wait(5) # seconds
self.driver.set_window_size(800, 800)
self.driver.set_window_position(0, 0)

Upvotes: 0

Moshisho
Moshisho

Reputation: 2981

For Java:

//imports:
import java.awt.*;
import java.awt.event.InputEvent;

driver = new FirefoxDriver();

//drag & drop to my right screen in debug mode:
boolean isDebug = java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("jdwp") >= 0;

if(isDebug) {
  Robot robot = new Robot();

  robot.mouseMove(800, 10);
  robot.mousePress(InputEvent.BUTTON1_MASK);
  robot.mouseMove(2800, 100);
  robot.mouseRelease(InputEvent.BUTTON1_MASK);
  driver.manage().window().maximize();
}

driver.manage().window().setPosition(point); didn't work for me.

Upvotes: 0

Ole Albers
Ole Albers

Reputation: 9305

I use this not very fancy, but quick method to achieve that. Because I do not only want it on my second screen but also maximized I don't have to be too precise on the coordinates. Any X-coordinate above 2000 is usually on the second screen for all my dev machines: (This example uses chromedriver but works with any IWebDriver)

chrome = new ChromeDriver();
chrome.Manage().Window.Position = new System.Drawing.Point(2000, 1); // To 2nd monitor. 
chrome.Manage().Window.Maximize();

Upvotes: 6

wailord
wailord

Reputation: 427

In python:

browser = webdriver.Chrome()
browser.set_window_position(2000, 0)

Upvotes: 18

csvan
csvan

Reputation: 9474

If you are running Windows 7 or later, you can (hack-ishly) achieve this by setting the desired display as your main display under "Change Display Settings". Any new browser windows will now open in this one. Keep in main that this of course affects your toolbar etc, which may or may not be annoying. It is certainly less annoying than having browser windows thrown in your face on a steady basis.

Upvotes: 1

Sergii Pozharov
Sergii Pozharov

Reputation: 17848

It is very useful for debugging to run tests on a secondary monitor. Unfortunately you cannot specify on which monitor browser should be opened. The options are:

  1. Open browser and drag it to the desired monitor, then close it. Next time it will be opened on that monitor
  2. Use software like Ultramon to control on which monitor to start browser.

Also you can use a virtual machine that will be run on the second monitor - just drag it there.

Upvotes: 2

mstrange
mstrange

Reputation: 609

Two options (this is for the wrapper):

  1. Use the Selenium Driver's window positioning commands:

    var monitor = Screen.FromPoint(new Point(Screen.PrimaryScreen.Bounds.Right + 1, Screen.PrimaryScreen.Bounds.Top));
    
    var seleniumDriver = new ChromeDriver(options);
    seleniumDriver.Manage().Window.Position = new Point(monitor.Bounds.X, monitor.Bounds.Y);
    
    var coypuDriver = new MultimonWebDriver(seleniumDriver, Browser.Chrome);
    var rv = new BrowserSession(sessionConfiguration, coypuDriver);
    
  2. Configure the Driver with a command line argument. I prefer this because solution #1 causes a flicker from the driver's server showing the window before processing the move command:

    var monitor = Screen.FromPoint(new Point(Screen.PrimaryScreen.Bounds.Right + 1, Screen.PrimaryScreen.Bounds.Top));
    
    var options = new ChromeOptions();
    options.AddArgument(String.Format("--window-position={0},{1}", monitor.Bounds.X, monitor.Bounds.Y));
    
    var seleniumDriver = new ChromeDriver(options);
    var coypuDriver = new MultimonWebDriver(seleniumDriver, Browser.Chrome);
    var rv = new BrowserSession(sessionConfiguration, coypuDriver);
    

where MultimonWebDriver is simply exposing access to the protected constructor:

public class MultimonWebDriver : SeleniumWebDriver
{
  public MultimonWebDriver(IWebDriver webDriver, Browser browser) : base(webDriver, browser)
  {
  }
}

Upvotes: 14

victorgp
victorgp

Reputation: 882

My solution is install an VNC server, open the VNC in one of the monitors and launch selenium through the VNC. Then you have always one monitor for the browsers of selenium without the annoying windows opened each time a test is launched

Upvotes: 0

Related Questions