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

后端 未结 17 2502
小蘑菇
小蘑菇 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);
      }
      

提交回复
热议问题