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

后端 未结 17 2210
小蘑菇
小蘑菇 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:54

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

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

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

    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)

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

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

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

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