Selenium is a tool that can be used as part of a testing framework for automating web interactions.
Key takeaways:
Selenium is a powerful tool for automating web interactions, allowing developers to simulate user behavior on web pages.
Installation of selenium webdriver and chromedriver, typically done via npm.
Webdriver is used to create a chrome webdriver instance to navigate pages, interact with elements, and retrieve text (e.g., from
<h1>
tags).Supports user actions (clicks, form submissions), alert handling, and content verification to streamline testing processes.
Selenium is a powerful tool for automating web interactions, allowing developers and testers to simulate user behavior on web pages. In this Answer, we’ll walk through the process of automating web page interactions using selenium with chrome webdriver.
To get started, you’ll need to install selenium webdriver and chromedriver. Here’s how to do it:
Install selenium webdriver: You can install selenium Webdriver using npm (node package manager) by running the following command:
npm install selenium-webdriver
Install chromedriver: Chromedriver is a webdriver implementation for chrome. You can download the appropriate chromedriver version for your chrome browser from the
npm install chromedriver
Once you have selenium webdriver and chromeDriver installed, you can create a webdriver instance for chrome.
const {By,Key,Builder} = require("selenium-webdriver"); const chrome = require('selenium-webdriver/chrome'); require("chromedriver"); async function example(){ const options = new chrome.Options(); options.addArguments('--no-sandbox'); const driver = new Builder() .forBrowser('chrome') .setChromeOptions(options) .build(); await driver.get("https://educative.io"); const element = await driver.findElement(By.tagName("h1")); const text = await element.getText(); console.log("Element text:", text); await driver.quit(); } example()
Line 8: This line creates a chrome options object, which will be used to configure how the chrome browser behaves.
Lines 11–14: We initialize a new instance of the selenium webdriver, configuring it to use chrome with the specified options.
Line 20: We navigate the webdriver to the "https://educative.io"
url.
Line 23: We find the HTML element with the name tag "h1"
.
Line 27: We retrieve the text of the h1
element present on web page using the function getText
and store it in the text
variable.
Line 30: We safely quit the webdriver, closing the browser session.
After running the above code, the chrome browser will be launched in the background and display the h1
text in the terminal.
With the webdriver instance set up, you can interact with various web elements on the page using selenium’s methods.
Selenium webdriver allows you to perform common user actions such as clicking buttons and submitting forms:
await driver.findElement(By.className("icon-default")).click();
You can handle JavaScript alerts and other pop-ups using selenium’s switchTo().alert()
method:
const alert = await driver.switchTo().alert();await alert.accept(); // To accept the alert
You can verify page content by asserting the text of specific elements or checking for the presence of certain elements:
const element = await driver.findElement(By.id("elementId"));const text = await element.getText();console.log("Element text:", text);
By following these steps, we can automate web page interactions using selenium with chrome webdriver. This allows us to streamline testing processes and simulate user behavior efficiently.
Ready to elevate your testing skills?
Join our mastering selenium path today! Dive deep into browser automation and functional testing across modern web browsers. With hands-on experience in react, node.js, and java, you’ll design your own testing framework and tackle multiple test cases.
Haven’t found what you were looking for? Contact Us
Free Resources