Loading Google on Chrome with Selenium

Krae Wind
2 min readSep 24, 2021

Step 1: Create a new directory

Step 2: (This blog post is using JavaScript and Node Package Manager) Open terminal and cd into the directory and run ‘npm init’.

Step 3: Run ‘npm i selenium-webdriver’

Step 4: Download the correct driver for Chrome here

Step 5: Move the driver to the directory you created in step 1

Step 6: Using your favorite code editor, add the following code to a new file in the main directory called ‘chrome.js’:

Step 7: Run ‘node chrome.js’ in your terminal from the main directory and watch the magic happen!

Break down of step 6:

On line 1, we pull in some methods from selenium-webdriver package.

On line 3, we create a function called openChrome that is ASYNCHRONOUS. It has to be asynchronous so that selenium knows to wait for one command to complete before executing the next.

Inside the function on line 4, we create the driver which is an instance of a Builder (The imaginary robot that will run or ‘drive’ our commands in the browser) and tell the code to AWAIT until the driver is built before moving on.

On line 5, we tell the driver to go to google’s homepage by giving it the URL and then AWAIT for the page to start loading before going to the last step.

On line 6, we tell the driver to find an element (the search bar on google’s homepage) via its xpath and then send the keys (or type) ‘Hello World!’

On line 7, we close the functions and call it!

--

--