Site icon TestingDocs.com

Running a Python Selenium Script

Introduction

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

Walkthrough

The selenium.webdriver module provides all the driver implementation. The test uses the 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 the 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()

driver.get("https://www.google.com")

element = driver.find_element_by_name("q")

element.clear()

element.send_keys("selenium")

element.send_keys(Keys.RETURN)

# implicit wait

driver.implicitly_wait(45)

#Make sure you get some result with selenium

assert "selenium" in driver.page_source

print("Sample Test Completed.")

# close driver

driver.close()


Test Output

 

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

 

 

Exit mobile version