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

后端 未结 17 2209
小蘑菇
小蘑菇 2021-01-31 15:53

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

相关标签:
17条回答
  • 2021-01-31 16:33

    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)
    
    0 讨论(0)
  • 2021-01-31 16:35

    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();
    
    0 讨论(0)
  • 2021-01-31 16:36

    Two options (this is for the coypu c# 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)
      {
      }
    }
    
    0 讨论(0)
  • 2021-01-31 16:37

    In python:

    browser = webdriver.Chrome()
    browser.set_window_position(2000, 0)
    
    0 讨论(0)
  • 2021-01-31 16:40

    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

    0 讨论(0)
  • 2021-01-31 16:44

    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

    0 讨论(0)
提交回复
热议问题