TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Python

Running a Python Selenium Script

Python Tutorials

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.

 

 

Related Posts

Download Python Windows 11

Python /

Download & Install Python on Windows 11

PyCharm CE

Python /

Install PyCharm on Ubuntu Linux

Python Install on CentOS Linux

Python /

Install Python on CentOS

What is Python?

Python /

What is Python?

Install Python

Python /

Install Python 3.6 on Windows

‹ Read file in Python› Python language bindings for Selenium

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version