selenium
  1. selenium-webdriver-introduction

WebDriver Introduction

Syntax

To instantiate a WebDriver object, we can use the following syntax:

from selenium import webdriver

# For Firefox:
driver = webdriver.Firefox()

# For Chrome:
driver = webdriver.Chrome()

# For Edge:
driver = webdriver.Edge()

Example

Let's see an example of using webdriver to open a webpage and perform a search:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.google.com")

search_bar = driver.find_element_by_name("q")
search_bar.send_keys("Selenium WebDriver")
search_bar.submit()

print(driver.title)

driver.quit()

Output

The above code will open a new Chrome browser window and navigate to https://www.google.com. It will then search for "Selenium WebDriver", print the title of the results page, and quit the browser window.

Explanation

The WebDriver is a tool for automating web application testing. It provides a way to interact with web elements on a web page and perform actions such as clicking buttons, filling out forms, and navigating between pages.

In the example above, we used webdriver to open a new Chrome browser window and navigate to Google. We then located the search bar on the page using its name attribute and sent a search query to it using send_keys(). We submitted the search query using submit(). Finally, we printed the title of the results page using title.

Use

The WebDriver is commonly used for:

  • Automated testing of web applications
  • Scraping data from websites
  • Automating tasks related to web browsing

Important Points

  • The WebDriver library provides a way to interact with web elements on a web page and perform actions.
  • The WebDriver has specific methods for locating HTML elements such as find_element_by_name(), find_element_by_id(), and find_element_by_css_selector().
  • We need to use specific WebDriver for each browser like Chrome, Firefox, IE, etc.
  • We can use WebDriver for testing the web application and automation tasks.

Summary

In this article, we learned about the webdriver module in Selenium and how to use it to perform various actions on a web page. We also discussed some important points related to the WebDriver and its use cases.

Published on: