Selenium and Python: Initial Setup and Writing the First Test

Altar Kocoglu
Orion Innovation techClub
5 min readJul 26, 2024
Figure 1: Connection between Python and HTTP [1]

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]

Figure 2: Selenium

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]

Figure 3: Selenium and Python

Writing the First Test

  1. Importing Necessary Libraries

To begin, we import the required libraries:

Figure 4: Importing Necessary 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:

Figure 5: Launch the Chrome browser

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]

Figure 6: Specify the Page to Open in the Browser

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]

Figure 7: Waiting for the Search Box to Load

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:

Figure 8: Finding and Clearing the Search Box

6. Performing a Search

The “send_keys” method inputs the text “orion innovation” and “keys.ENTER” simulates pressing the Enter key.[4]

Figure 9: Performing a Search

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”.

Figure 10: Waiting for the Result Link to Load

8. Finding and Clicking the Link

The “find_element” method locates the link containing “Orion” text, and the “click” method clicks the link.

Figure 11: Finding and Clicking the Link

9. Waiting Period

We introduce a short wait to allow the page to load using the “sleep” method:

Figure 12: Waiting Period

10. Closing the Browser

Finally, we close the browser with quit():

Figure 13: Closing the Browser

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)

--

--