Walkthrough: Chrome Start with Arguments

When using Selenium for automation, the browser launched by WebDriver (here, Chrome is used as an example) is a pure instance without Chrome extensions. And we often have some special requirements for the browser, we need to add startup parameters to WebDriver. This article mainly introduces:

  • Chrome configuration items and its configuration method in Selenium-WebDriver
  • Scheme for configuring Chrome dedicated users for Selenium-WebDriver

Configuration method

In other language implementations of Selenium, such as Java and Python, Chrome configuration is controlled by the ChromeOptions class, but in JavaScript, the Options class in selenium-webdriver/chrome is used. The specific implementation is as follows:

Use Option to add configuration

First, if the startup parameters are not configured, the writing method of WebDriver call is as follows:

JavaScript
require('chromedriver');
const {Builder} = require('selenium-webdriver');

driver = new Builder().forBrowser('chrome').build();

If we need Chrome to start in a maximized state, we need to add the --start-maximized startup parameter, which can be added using the Option.addArguments() method.

JavaScript
require('chromedriver');
const {Builder} = require('selenium-webdriver');
const {Options} = require('selenium-webdriver/chrome');

let options = new Options();
options.addArguments('--start-maximized');
driver = new Builder().setChromeOptions(options).forBrowser('chrome').build();

If you want to start the browser in incognito mode, you need to use the --incognito startup parameter, and add this parameter to options before instantiating the driver:

JavaScript
options.addArguments('--incognito');

Common configuration items

Chrome has many configuration items. Adding startup parameters is a relatively simple configuration. In addition, you can configure the browser's functions and preferences (Preference), executable file path, proxy, etc.

Common startup parameters

There are many startup parameters that can be configured, and the full version can be accessed from this website. But here we only list some commonly used parameters.

JavaScript
options.addArguments('--user-data-dir=[path]') // Set user data folder
options.addArguments('--headless') // headless mode
options.addArguments('--disable-gpu') // Disable GPU acceleration
options.addArguments('--start-maximized')// The browser is maximized
options.addArguments('--window-size=1280x1024') // Set browser resolution (window size)
options.addArguments('log-level=3') // Logging level
// info(default) = 0
// warning = 1
// LOG_ERROR = 2
// LOG_FATAL = 3
options.addArguments('--user-agent=""') // Set the User-Agent of the request header
options.addArguments('--incognito') // Incognito mode (incognito mode)
options.addArguments('--hide-scrollbars') // hide the scrollbars to deal with some special pages
options.addArguments('--disable-javascript') // disable javascript
options.addArguments('--blink-settings=imagesEnabled=false') // Do not load images, improve speed

/*--Deprecated--*/
options.addArguments('--disable-infobars') // Disable the prompt that the browser is being controlled by the automatic test program

The parameter that disables the prompt of Chrome is under automatic test program control has been removed in a change due to malicious abuse.

Add extension

Use the Option.addExtension(path) method to add extensions to the browser, and path is the path of the extension's .crx file. The added extension will be installed automatically when the browser is started.

Configure a set of browsers for Selenium automation

When writing Web automation with Selenium, you may be annoyed by the fact that WebDriver restarts a new clean browser every time, which may cause a large number of configuration scripts (such as login, registration, etc.) to be executed before the start of the business test script. Wait). And if you ask WebDriver not to start a clean browser every time, but to start a browser with user data that is the same as usual, you can use existing cookies or keep the login status when performing business tests. To avoid repeated configuration operations.

The following describes how to make WebDriver use user data.

Start the browser directly with the default user

In fact, in the Common Startup Parameters section, we introduced the parameters for configuring user data in the first line, namely --user-data-dir=[path], as long as it is specified in the Chrome installation directory The User Data folder, you can open the browser you usually use. The code to add configuration is as follows:

JavaScript
options.addArguments('--user-data-dir=C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data');

Please replace username with the actual user's Windows login name, so that the above path points to the correct existing path. Then start WebDriver and you can see a set of familiar browsers that are the same as usual. Of course, more often you will only see a string of errors as follows:

(node:14272) UnhandledPromiseRejectionWarning: InvalidArgumentError: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir

This is usually because a browser instance is already using the user data folder in the background, which will cause read-write conflicts. But this is actually just because you have opened a browser, and the two browsers use the same user data folder; or there are still related processes using the user data in the background (Chrome will keep browsing in the background in order to ensure the running speed. Device extension, and the extension uses user data). Therefore, it may be necessary to terminate these Chrome processes to start successfully. This is very troublesome. Of course, another solution is provided below, which can better solve this problem.

Start the browser with the new user

In the previous section, the reason for the error is because another instance occupies the user data folder, so we only need to create another user data folder-that is, create a new browser user, the specific method is as follows:

New users

  1. Open Chrome on your computer.
  2. Click "Personal Information" in the upper right corner! Personal Information.
  3. Click Manage users.
  4. Click Add user.
  5. Select the name and photo.
  6. Click Save. The system will open a new window and ask you to turn on the synchronization function, there is no need to open it here.

At this time, you can visit the original user data folder again, that is C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data, you can see the new name appears in the directory It is the folder of Profile 1 (the number depends on how many accounts you have created).

Use the newly added user data folder

We change the data folder started by WebDriver to the newly added user data path, namely C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1, and then Start it and you can see that a new browser is opened.

But you must have noticed that the "personal data" in the upper right corner here! personal data is still empty, instead of the avatar set when the user was just added. This is because when Chrome is launched for the first time from the command line, it will automatically create a \Default folder under the current user data folder and use it, so the user will still be an empty user. Fortunately, this does not affect WebDriver's saving of user data.

You can visit chrome://version in the address bar to view, the path in the personal data path column is C:\\Users\\username\\AppData\\Local \\Google\\Chrome\\User Data\\Profile 1\\Default. At this time, you will also find that the browser stays longer after startup and before performing operations. This is because the browser is opened before loading user data, so it will take more time. It is also for this reason that WebDriver opens a pure browser every time to ensure the running speed.

Check whether the browsing data can be saved

Then you can allow WebDriver to use user data normally. For example, you can visit GitHub to log in to your account, and then open it again and you can see that you are logged in and logged in Up.

This method can also effectively isolate user data. Take Github as an example. You may have your own account, but there is another test account. In the original situation, it may be necessary to switch accounts on Github frequently, but now it is not necessary. The login status of the two accounts is stored in different user data folders, and there is no conflict.

How to manually open the newly added user browser

After studying the Chrome shortcut on the desktop, you can notice that the command to start Chrome for a specific user is "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile-directory=" Profile1". The WebDriver just started is actually the newly created Default user under Profile, so we change the command to "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile- directory="Profile1\Default". Of course, you can also directly change the target to the above command in the shortcut.

Chinese version click here.

results matching ""

    No results matching ""