Unnamed
Unnamed

Reputation: 1790

Mac selenium webdriver chrome window always starts with a small window

I am having an issue using chrome and selenium 2 webdriver. The issue is that when I start my browser session using the chromedriver it always starts in a window that is less than half the size of the available screen width. Because I am doing tests on a page that changes when the screen size changes, my tests fail because I'm trying to drag an element from the top of the page and drop it to an element that is at the bottom of the page. I get a scrolling error. But if the window is maximized, then I don't get this error. But the problem is, every time chrome starts a new session via chrome driver it always starts in a small window. I have explored many different options to get the browser to start maximized:

What other options could I explore in order to accomplish this? The issue is that the window is too small? What can I do to automaticallly maximize the window every time chrome starts via webdriver on a mac?

Upvotes: 10

Views: 11184

Answers (9)

Lukyanov Mikhail
Lukyanov Mikhail

Reputation: 525

I use webdrivermanager and selenide. For maximize the window Configuration.startMaximized = true

import io.github.bonigarcia.wdm.ChromeDriverManager;
import com.codeborne.selenide.Configuration;
import io.github.bonigarcia.wdm.DriverManagerType;

import static com.codeborne.selenide.Selenide.open;

public class Main {

    public static void main(String[] args) {
        ChromeDriverManager.getInstance(DriverManagerType.CHROME).version("76.0.3809.126").setup();
        Configuration.baseUrl = "https://www.google.com/";
        Configuration.startMaximized = true;
        open("/");

Upvotes: 0

Bipin Kumar Chaurasia
Bipin Kumar Chaurasia

Reputation: 418

Please check following solution, It will work for you.

System.setProperty("webdriver.chrome.driver", driverPath);
ChromeOptions options= new ChromeOptions();
options.addArguments("start-fullscreen");

Upvotes: 0

Sagar007
Sagar007

Reputation: 848

I have same problem on MAC.

I have tried below options but not works:

  1. driver.manage().window().maximize();

  2. Here

    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("--start-maximized");
    driver = new ChromeDriver(chromeOptions);
    

Finally I have found below solution which works on all OS :

ChromeOptions options = new ChromeOptions();
options.addArguments("start-fullscreen");

Let me know If any query.

Upvotes: 0

Chirag Khimani
Chirag Khimani

Reputation: 121

It's very old post but still I faced this issue today and resolved using below code.

java.awt.Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
org.openqa.selenium.Point position = new Point(0, 0);
driver.manage().window().setPosition(position);
org.openqa.selenium.Dimension maximizedScreenSize =
            new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
driver.manage().window().setSize(maximizedScreenSize);

Upvotes: 0

dan2006112
dan2006112

Reputation: 21

The best solution I found until now is the following:

after starting the browser run the following code

Point targetPosition = new Point(0, 0);
driver.manage().window().setPosition(targetPosition);

Dimension targetSize = new Dimension(1920, 1080); //your screen resolution here
driver.manage().window().setSize(targetSize);

I hope it helps. I managed to maximize Chrome under MAC using this solution.

Upvotes: 2

Marquee
Marquee

Reputation: 1826

I solved this issue by executing an apple script. This, of course ties you to execution on a Mac, but that was okay for us. Windows doesn't have the same maximization problem for Chrome, so if you care you could do an OS check.

I found the apple script I used here. I copied it below in case the link ever breaks:

tell application (path to frontmost application as string) 
to copy (run script "tell application \"Finder\" to desktop's window's bounds") 
to bounds of window 1

I used this script (inspired by this question) to activate the browser window first:

tell application "Google Chrome"
    activate
end tell

I executed in Java using this command:

Runtime.getRuntime().exec(scriptPath)

Hope this helps someone.

Upvotes: 1

KailuoWang
KailuoWang

Reputation: 1314

What you described here was also the case for me. Following to this article, you can set preference for chromedriver when starting it.

Ruby:

profile = Selenium::WebDriver::Chrome::Profile.new
profile['browser.window_placement.top'] = 0
profile['browser.window_placement.left'] = 0
profile['browser.window_placement.right'] = 1024
profile['browser.window_placement.bottom'] = 768
Selenium::WebDriver.for :chrome, profile: profile

This worked on both Windows and Mac. On Java though, I couldn't verify the API for setting the preference for chromedriver. Here is a question asked about it but seems no answer has been found yet.

Upvotes: 2

yurodivuie
yurodivuie

Reputation: 116

driver.manage().window().setSize() will not currently work with ChromeDriver (as of v19). There is an active chromium issue relating to this feature request.

The current workaround generally recommended is to use DesiredCapabilities to set the window size, but you're right that it doesn't seem to work on the Mac at all.

The only solution I've found is to open a new window using JavaScript and resize the new window in the same fashion. It's described deep in this thread on the topic. The relevant workaround is shown in the code below:

JavascriptExecutor js = ((JavascriptExecutor)driver);
js.executeScript("window.open('','testwindow','width=400,height=200')");
driver.close();
driver.switchTo().window("testwindow");
js.executeScript("window.moveTo(0,0);");
js.executeScript("window.resizeTo(1280,800);");

I'm afraid it's quite clunky, but it worked for me locally using chrome on the Mac (Lion).

Upvotes: 4

Amey
Amey

Reputation: 8548

driver.manage().window().setSize() is the method you want to use

Upvotes: 1

Related Questions