Selenium and Python: Initial Setup and Writing the First Test
What is Selenium?
Selenium is a popular open-source software suite for automating and testing web applications. Selenium interacts with various browsers to open web pages, fill out forms, click on links, and perform other user actions without human intervention.
Selenium can be used with Java, C#, Python, Ruby, and many other programming languages. It is especially preferred in continuous integration and deployment (CI/CD) processes of web applications.[2]
How to Install Selenium?
Selenium’s installation is quite simple. First, you should install the Selenium library for Python by running “pip install selenium” in your terminal. Additionally, you can use the webdriver-manager package to simplify the process of downloading and setting up the WebDriver. To do this, run the following command:
pip install webdriver-manager
Once installed, you can use it in your Python code to automatically manage the WebDriver.
This setup is done on a PC running the Windows operating system. This code will automatically download and set up the latest version of ChromeDriver. After completing these steps, you can easily begin your web automation projects.[3]
Writing the First Test
- Importing Necessary Libraries
To begin, we import the required libraries:
- Selenium Library: Used to automate web browsers.
- webdriver: Used to start the browser.
- By: Used to locate HTML elements.
- Keys: Used to simulate keyboard keys.
- WebDriverWait: Used to wait for a specified duration.
- expected_conditions (EC): Used to wait for certain conditions to occur.
- time: Used to add time delays.[4]
2. Launching the Browser
Next, we launch the Chrome browser using The “webdriver.Chrome()” method:
3. Specify the Page to Open in the Browser
At this stage, we use the get command in the browser and type the URL of the site we wish to open. The get method navigates to the specified URL and opens the site we want to open.[4]
4. Waiting for the Search Box to Load
We wait for the search box to become visible using “WebDriverWait”, which waits for the search box to load for up to 5 seconds.[4]
5. Finding and Clearing the Search Box
Once the search box is loaded, we locate it by the “find_element” method and clear it by using the “clear” method:
6. Performing a Search
The “send_keys” method inputs the text “orion innovation” and “keys.ENTER” simulates pressing the Enter key.[4]
7. Waiting for the Result Link to Load
We wait for the search results containing “Orion” text to load for up to 5 seconds to load using the “WebDriverWait”.
8. Finding and Clicking the Link
The “find_element” method locates the link containing “Orion” text, and the “click” method clicks the link.
9. Waiting Period
We introduce a short wait to allow the page to load using the “sleep” method:
10. Closing the Browser
Finally, we close the browser with quit():
RESULTS OF THE FIRST TEST
As a result of these operations, the Chrome browser is automatically launched and the Google homepage opens. Then, a search query for “orion innovation” is performed, and among the search results, the first link containing the word “Orion” is found and clicked. The page opens and is displayed for 10 seconds, after which the browser page is closed.
This code is used to automatically access a specific website, perform a search, and click on one of the results. It can be particularly useful in projects involving web browser automation.
Conclusion
In this presentation, we have explored the significance of Selenium in automating and testing web applications, and how it can be integrated with Python. We examined what Selenium is and the components it comprises. Then, we learned step-by-step how to set up Selenium in a Python environment and wrote our first test.
With the extensive API offered by Selenium, we can simulate user interactions in web applications, automate test scenarios, and manipulate web page elements effortlessly. These capabilities enhance quality assurance in software development processes and reduce the burden of manual testing.
Having completed our first test, we have laid a solid foundation for creating more complex test scenarios in the future. By leveraging the power of automation testing with Selenium, you can elevate the quality standards of your projects and develop more reliable software over time.
Thank you!
References
[1] “How to Launch an HTTP Server in One Line of Python Code”, Real Python. https://realpython.com/python-http-server/ (May 22. 2023)
[2] “What is Selenium?”, BrowserStack. https://www.browserstack.com/selenium
[3] “Installing Python bindings for Selenium”, Selenium Python Docs. https://selenium-python.readthedocs.io/installation.html
[4] “Selenium Tutorial”, GitHub. https://github.com/techwithtim/SeleniumTutorial/blob/main/main2.py (Nov 19, 2023)