Running a Python Selenium Script

In this example, we will run a simple python selenium script using Firefox browser. The code searches Google website for a keyword and asserts the text in the search result.

Walk through

The selenium.webdriver module provides all the driver implementation. The test uses Firefox browser to launch. We can use other browsers as well. Make sure you place the geckodriver.exe under the project.

driver = webdriver.Firefox() this instantiates the firefox driver.The driver.get method will navigate to Google website as specified by the URL.

An assertion to confirm that text selenium is in the page source.

driver.find_element_by_name(“q”) finds the element. If you inspect Google webpage you can locate the input textbox with its name attribute q. send_keys method in the code, simulates entering keys using your keyboard.


Python Selenium script:

# Sample Selenium Python Script

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

# Firefox driver

driver = webdriver.Firefox()


element = driver.find_element_by_name("q")




# implicit wait


#Make sure you get some result with selenium

assert "selenium" in driver.page_source

print("Sample Test Completed.")

# close driver




You can run the script by right click and choose Run <scriptname> option.

Related Posts