Python and Automation

 

Python and Automation Testing: A Guide to Selenium WebDriver

Python has become a popular choice for automation testing, and one of the most widely used tools for testing web applications is Selenium WebDriver. In this guide, we will cover the basics of Selenium WebDriver and show you how to use Python to automate web testing.

1. Introduction to Selenium WebDriver

Selenium is a suite of tools used for web automation testing. Selenium WebDriver is one of the most popular tools in the suite, used for automating web browsers. WebDriver allows you to automate web interactions such as clicking buttons, entering text, and navigating pages.

2. Setting Up Selenium WebDriver with Python

Before we can start using Selenium WebDriver with Python, we need to set up the environment. The first step is to install the Selenium package using pip:

pip install selenium

Next, we need to download the WebDriver for the browser we want to automate. Selenium supports several browsers, including Chrome, Firefox, Safari, and Internet Explorer. We will use Chrome for this example. You can download the Chrome WebDriver from the official Selenium website: https://sites.google.com/a/chromium.org/chromedriver/downloads

Once you have downloaded the Chrome WebDriver, extract the file and place it in a directory that is included in your system’s PATH environment variable. This will allow Selenium to find the WebDriver when we launch the Chrome browser.

3. Basic Selenium WebDriver Commands

Let’s start with a basic example that navigates to a website and prints the page title:

from selenium import webdriver

# create a new Chrome browser instance
browser = webdriver.Chrome()

# navigate to the website
browser.get('https://www.google.com')

# print the page title
print(browser.title)

# close the browser
browser.quit()

In this example, we create a new instance of the Chrome browser using the webdriver.Chrome() method. We then use the get() method to navigate to the website. Finally, we print the page title using the title property and close the browser using the quit() method.

4. Interacting with Web Elements

WebDriver allows us to interact with web elements such as buttons, text boxes, and links. We can use the find_element_by_*() methods to locate the element we want to interact with. For example, to enter text into a text box, we can use the find_element_by_name() method and the send_keys() method:

from selenium import webdriver

# create a new Chrome browser instance
browser = webdriver.Chrome()

# navigate to the website
browser.get('https://www.google.com')

# find the search box and enter text
search_box = browser.find_element_by_name('q')
search_box.send_keys('Python')

# submit the search form
search_box.submit()

# close the browser
browser.quit()

In this example, we locate the search box using the find_element_by_name() method and enter the text “Python” using the send_keys() method. We then submit the search form by calling the submit() method on the search box element.

5. Advanced WebDriver Commands

WebDriver also allows us to perform more advanced actions such as clicking on elements, waiting for elements to load, and taking screenshots of the page. Here are some examples:

Clicking on an Element

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# create a new Chrome browser instance
browser = webdriver.Chrome()

# navigate to the website
browser.get('https://www.google.com')

# find the search box and enter text
search_box = browser.find_element_by_name('q')
search_box.send_keys('Python')

# find the search button and click it
search_button = browser.find_element_by_name('btn

6. Locating Elements on a Web Page

Selenium offers several methods for locating elements on a web page. Here are some of the most commonly used methods:

  • find_element_by_id: Locates an element by its ID attribute.
  • find_element_by_name: Locates an element by its Name attribute.
  • find_element_by_class_name: Locates an element by its Class attribute.
  • find_element_by_xpath: Locates an element using an XPath expression.
  • find_element_by_link_text: Locates an anchor element (a) by its visible text.
  • find_element_by_partial_link_text: Locates an anchor element by a partial match of its visible text.
  • find_element_by_tag_name: Locates an element by its tag name.
  • find_element_by_css_selector: Locates an element using a CSS selector.

For example, to locate the search box on Google.com, you can use the find_element_by_name method as follows:

search_box = driver.find_element_by_name('q')

7. Executing JavaScript

Selenium also allows you to execute JavaScript code on a web page using the execute_script method. This can be useful for performing advanced operations that are not supported by Selenium’s built-in methods.

For example, to scroll to the bottom of a web page, you can use the following JavaScript code:

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

8. Handling Pop-Ups and Alerts

Selenium can handle pop-ups and alerts that appear on a web page using the switch_to method. You can switch to an alert using the following code:

alert = driver.switch_to.alert

You can then interact with the alert using its methods, such as accept or dismiss.

9. Taking Screenshots

Selenium allows you to take screenshots of a web page using the save_screenshot method. For example, to save a screenshot of the current page as a PNG file, you can use the following code:

driver.save_screenshot('screenshot.png')

10. Conclusion

In this article, we have seen how to use Selenium WebDriver with Python to automate web browser testing. We have covered the basics of setting up Selenium, navigating web pages, locating elements, interacting with web elements, executing JavaScript, handling pop-ups and alerts, and taking screenshots. By combining Selenium with Python, you can create powerful and flexible test automation frameworks that can save you time and effort in the long run.

Hire top vetted developers today!