Selenium Python Bindings— Automated Web Testing on Brave with Python

Automating web applications for testing purposes

Passivebot
7 min readApr 24, 2021

Overview

Questions

  • What are Selenium Python bindings?
  • How do you automate Brave using Selenium Python bindings?
  • What is DOM navigation?
  • How do you switch through windows and frames when automating Brave with Selenium?
  • What are explicit and implicit waits?

Objectives

  • Learn about Selenium Python bindings.
  • Understand the process of automating a Brave instance with Selenium Python bindings.
  • Learn how to traverse web pages using DOM navigation.
  • Learn how to switch through windows and frames when using Selenium with Brave.
  • Understand the differences between explicit and implicit waits.

Selenium Framework

Selenium is an open-source web automation framework that lets us interact with HTML elements on any web page. It’s a great framework for making and testing bots and automated test scripts for websites.

Selenium is flexible and offers functions to perform basic browser manipulation like dragging and dropping, clicking buttons, filling out forms, grabbing data from specific HTML tags, finding elements in the source code, and even reading the entire page’s source code. Anything you want to do on a web page, you can automate that with Selenium.

Since Selenium WebDriver supports various programming languages Selenium Python bindings are the specific API framework needed to use Python with Selenium WebDriver.

Pre-requisites for running Selenium tests with Python

The easiest way to install Selenium on a Python environment is through the installer pip.

pip install selenium

While the installation of Selenium makes its functionality available to you, you need an additional driver for it to be able to interface with Brave. You’ll want to install WebDriver Manager through installer pip next.

pip install webdriver-manager

Webdriver Manager for Python eliminates needing to manually download the specific Chrome drivers required to interface with Brave.

How to run your first test using Selenium and Python?

Once you have completed the pre-requisites section, you are ready to start your first Selenium test with Python.

1. First import the webdriver and Keys classes from Selenium and webdriver_manager.chrome from ChromeDriverManager.

The webdriver class will connect you to a browser’s instance, which we will shortly cover. The Keys class lets you emulate the stroke of keyboard keys, including special keys like “Ctrl” and “Return”. ChromeDriverManager class will take care of automatically downloading the correct version of ChromeDriver for your current Brave version.

2. Next, create a new instance of Brave and interface it with Selenium through the driver Web Driver Manager automatically provides.

You first want to define the location of Brave, and then set it as a binary location parameter for ChromeOptions. Whenever you initiation a new instance of Brave, WebDriver Manager will download a matching Chrome driver according to your browser's current version.

If you’re testing on your local machine, this opens an instance of Brave locally. This command lets you perform tests on it until you use the .close() method to end the connection to the browser.

3. Next, use the .get() method of the driver to load a website. You may also load a local development site as this process is equivalent to opening a window of Brave on your local machine, typing a URL, and hitting Enter. The .get() method not only starts loading a website but also waits for it to render completely before moving on to the next step.

4. Once the page loads successfully, you can use the .title attribute to access the textual title of the webpage. If you wish to check whether the title contains a particular substring, you can use the assert or if statements. For simplicity, let us print the title of the page.

The output is the following text –

Earn gift cards & Bitcoin passively

If you are running the test on a Python interpreter, you notice that the Brave browser window is still active. Also, a message on Brave appears underneath the address bar stating that automated software is controlling it at the moment.

5. Next, let us submit a query in the search bar on Google. First, you want to load Google using the .get() function. Then select the element from the HTML DOM and enter a value into it and submit the form by emulating the Return key press. You can select the element using its CSS class, ID, its name attribute, or even the tag name. If you check the source of the query search bar, you notice that the name attribute of this DOM element is “q”. Therefore, you can use the .find_element_by_name() method as follows to select the element.

6. Once the DOM element is selected, you first need to clear its contents using the .clear() method, enter a string as its value using the .send_keys() method and finally, emulate the press of the Return key using Keys.RETURN.

You notice in the window that these actions trigger a change in the URL with the search results in the window. To confirm the current URL of the window, you can use the following commands. End with the .close() method to close the automated Brave instance.

The following string is displayed –

https://www.google.com/search?q=Passivebot.com&source=hp&ei=YbV9YJXVI8iIwbkPp7y5mAc&iflsig=AINFCbYAAAAAYH3DcekE4ULNjcJ-gXfLmzX_uTVLCSwr&oq=Passivebot.com&gs_lcp=Cgdnd3Mtd2l6EANQqwlY-glgvgpoAHAAeACAAQCIAQCSAQCYAQCgAQGqAQdnd3Mtd2l6&sclient=gws-wiz&ved=0ahUKEwjViaHH4orwAhVIRDABHSdeDnMQ4dUDCAk&uact=5

To summarise the discussion, here is your first Brave automation test with Python. You can save it in the file brave_test.py and run python brave_test.py to run the test.

Navigating through HTML DOM Elements

Now that you have successfully run your first test in Selenium with Python, let’s look at various options to select DOM elements and interact with them. In the example above, we selected the search bar and queried for a string. Let’s explore element selection further.

In the example, we used the .find_element_by_name() method, which searches for the attribute name within the input HTML tag. We can also search for this term using other methods.

  • CSS ID: .find_element_by_id(“id-search-field”)
  • DOM Path: .find_element_by_xpath(“//input[@id=’id-search-field’]”)
  • CSS class: .find_element_by_class_name(“search-field”)

While the CSS ID is unique to every element by design, you may find multiple results when searching through the class name. Further, when you search through the DOM path of the element, you can be certain of what you are searching for.

Navigating through Windows and Frames

Automating Brave may require you to work with multiple windows and frames. Common use cases of working on new windows are social logins and accessing different Brave tabs. The .switch_to_window() method of the driver will help you to change the active window and work on different actions in a new window. The code that switches focus to a new window is:

If the value is not stored in the target attribute, you may use the window_handles attribute, which uniquely identifies all open windows in your driver. To view a list of all window handles, run the following:

Similarly, you can switch focus to a frame within a window through the .switch_to_frame() method. ReCAPTCHA’s are typically in their own frame separate from the websites. To switch back to the primary window after completing relevant actions, run the following:

Note that all actions within this section change the state of the driver and do not return anything. Therefore, you do not store the values in a variable and instead just need to call the methods.

Idle Time During Automation

While we have looked at various tests in static web applications, a single-page application may require you to wait for a specific time until you can act.

There are two types of waits in Selenium: implicit and explicit waits.

An implicit wait makes the driver wait for a particular time.

An explicit wait makes the driver wait for a specific action to be completed, like waiting for a website logo to load.

Final Thoughts

In this article, we did a step-by-step breakdown of automating Brave with Selenium Python bindings. Then we discussed DOM navigation and switching through windows and frames. Finally, we covered explicit and implicit waits.

Don’t have a Bitcoin wallet yet?
Start storing, trading, and earning cryptocurrencies with CoinBase.

For more tips and updates, follow me on:
Passivebot, Twitter, Facebook, and GitHub.

Bitcoin Cash donation: qqdxn2rk4ze9q2zp7vmcrlaw9tdqnlf0eu8mumkz3l

Bitcoin donation: 39n7ovER7BUb7qP7rwH8EsZ2gx99JN7mYV

This article is for informational purposes only. It should not be considered Financial or Legal Advice. Contained information may not be accurate at all times. Consult a financial professional before making any major financial decisions

--

--

No responses yet