Selenium - Starting a Browser Instance
Since we aim to automate a browser, naturally the first step is to be able to create a new browser window. In this chapter we will learn how to create a new browser window along with some basic configurations.
- , opens subtitles settings dialogsubtitles settings
- , selectedsubtitles off
- 2x
- 1.5x
- , selected1x
- 0.5x
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
Summary
To create a new browser window, first we need to import the Builder
class from selenium-webdriver
module:
jsconst { Builder } = require('selenium-webdriver');
Once it is imported, we need to import the relevant module based on our choice of browser. For-example, if we want to automate chrome, we need to import the chrome
module from the selenium-webdriver
folder:
const chrome = require('selenium-webdriver/chrome');
Following are the imports for other browsers:
js912345678// Microsoft Edgeconst edge = require('selenium-webdriver/edge');// Firefoxconst firefox = require('selenium-webdriver/firefox');// Internet Explorerconst ie = require('selenium-webdriver/ie');
Once we're done with the required imports, we can move on to writing the code for creating the browser window. It is important to note that almost all of the functions and methods provided by Selenium are asynchronous in nature, hence, when we are executing Selenium code, we need to enclose it in an asynchronous function.
We can create an inline asynchronous function using the following syntax:
js9123async () => {// selenium code here}
JavaScript allows us to execute the function directly without storing it anywhere, using the following syntax:
js9123(async () => {// selenium code here})();
Inside this function, we can add the following two lines of code, where path/to/webdriver.exe
represents the path to the webdriver which you downloaded for your choice of browser (for-example: chromedriver.exe
):
js912let chromeDriverPath = path.resolve('path/to/webdriver.exe');let service = new chrome.ServiceBuilder(chromeDriverPath);
Note that, for using the path
module, we need to import that as well:
jsconst path = require('path');
We can use the addArguments
method of browserName.Options()
class to define configurations for the browser before launch. For-example, the following code sets Chrome's language to English-UK
and disables the "Default Search Engine Selection Screen".
js9123let options = new chrome.Options().addArguments('--disable-search-engine-choice-screen').addArguments('--lang=en-GB');
Finally, for creating the browser window, we can use the Builder
class:
js912345let driver = await new Builder().forBrowser('chrome').setChromeService(service).setChromeOptions(options).build();
The forBrowser
method specifies which browser we are aiming to automate. The setChromeService
method accepts a ServiceBuilder
object, which contains the path to the webdriver.
setChromeOptions
is a method for configuring chrome, and it accepts the options
object. There are similar methods for other browsers as well, for-example, setEdgeOptions
, setIeOptions
, setSafariOptions
, etc.
The build
method initializes the window, and returns a reference which can be used for controlling what goes on inside the browser window.
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat