Python Selenium Basics

In this article we want to learn about Python Selenium Basics, so Selenium is a set of tools for browser automation, and it is mostly used for testing applications. but the usage of selenium is not limited to testing of applications, but it can also be used for screen scrapping and repetitive tasks automation in a browser.  so selenium supports automation for different browsers like firefox, internet explorer, google chrome, safari  and opera.

 

 

Python Selenium Basics

For working with the example of this tutorial you need some requirements, first you should have installed Python in your system, then we need to install Python Selenium and you can use pip for that like this, also you need to driver for specific browser.

 

 

Note: You can download the drivers from here.

Chrome: https://chromedriver.chromium.org/downloads
Edge: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Firefox: https://github.com/mozilla/geckodriver/releases
Safari: https://webkit.org/blog/6900/webdriver-support-in-safari-10/

 

 

 

To begin with Selenium, we first need to import the necessary modules and create an instance of a web driver. Below is an example of launching the Chrome browser:

 

 

After launching the browser, we can instruct Selenium to navigate to a specific URL. Let’s use the https://demoqa.com/login page as an example:

 

 

Selenium provides different methods to interact with web elements such as buttons, input fields and dropdowns. We can locate elements using different techniques, including XPath, CSS selectors and element IDs. This is an example of logging in by filling in the username and password fields:

 

 

 

Web pages may load at different speeds, and elements may appear dynamically. To ensure that our automation scripts work reliably, we can incorporate wait times to synchronize with the page loading process. This is an example of using an explicit wait to wait for an element to become clickable:

 

 

 

This is the complete code

 

 

Learn More on Python Selenium 

Leave a Comment