Skip to main content

Selenium With Python

To run Selenium tests using Python, clone the repository, or select and make use of one of the sample tests below to see our platform in action.

info

All the scripts below makes use of the grid access key. In order to run the test do the following

info

Replace <server> with the appropriate URL.

Chrome with Python Expand source

import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions


class TestFirefox(unittest.TestCase):
dc = {}
testName = 'Selenium Test on chrome'
driver = None

def setUp(self):
self.dc['testName'] = self.testName
self.dc['browserName'] = 'chrome'
# Specify the access key in order to run this test against the browser Grid
self.dc['accessKey'] = '<YOUR ACCESS KEY>'
self.driver = webdriver.Remote('<server>', self.dc)

def test_chrome(self):
self.driver.get("https://seetest.io")
WebDriverWait(self.driver, 10).until(
expected_conditions.presence_of_element_located((By.XPATH, "//*[text()='Manual']")))

manual_nav_link = self.driver.find_element_by_xpath("//*[text()='Manual']")
manual_nav_link.click()

automation_nav_link = self.driver.find_element_by_xpath("//*[text()='Automation']")
automation_nav_link.click()

webinar_footer_link = self.driver.find_element_by_xpath("//*[text()='Webinars']")
webinar_footer_link.click()

webinars_h2_title_text = self.driver.find_element_by_xpath("//h2[1]").text

print("The title of the first h2 is: " + webinars_h2_title_text)

def tearDown(self):
self.driver.quit()

if __name__ == '__main__':
unittest.main()

Firefox with Python Expand source

import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions


class TestFirefox(unittest.TestCase):
dc = {}
testName = 'Selenium Test on Firefox'
driver = None

def setUp(self):
self.dc['testName'] = self.testName
self.dc['browserName'] = 'firefox'
# Specify the access key in order to run this test against the browser Grid
self.dc['accessKey'] = '<YOUR ACCESS KEY>'
self.driver = webdriver.Remote('<server>', self.dc)

def test_firefox(self):
self.driver.get("https://seetest.io")
WebDriverWait(self.driver, 10).until(
expected_conditions.presence_of_element_located((By.XPATH, "//*[text()='Manual']")))

manual_nav_link = self.driver.find_element_by_xpath("//*[text()='Manual']")
manual_nav_link.click()

automation_nav_link = self.driver.find_element_by_xpath("//*[text()='Automation']")
automation_nav_link.click()

webinar_footer_link = self.driver.find_element_by_xpath("//*[text()='Webinars']")
webinar_footer_link.click()

webinars_h2_title_text = self.driver.find_element_by_xpath("//h2[1]").text

print("The title of the first h2 is: " + webinars_h2_title_text)

def tearDown(self):
self.driver.quit()

if __name__ == '__main__':
unittest.main()

Internet Explorer with Python Expand source

import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions


class TestIE(unittest.TestCase):
dc = {}
testName = 'Selenium Test on IE'
driver = None
def setUp(self):
self.dc['testName'] = self.testName
self.dc['browserName'] = 'internet explorer'
self.dc['accessKey'] = '<YOUR ACCESS KEY>'
# Specify the access key in order to run this test against the browser Grid
self.dc['initialBrowserUrl'] = 'https://seetest.io'
self.driver = webdriver.Remote('<server>', self.dc)

def test_ie(self):
self.driver.get("https://seetest.io")
WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, "//*[text()='Manual']")))

manual_nav_link = self.driver.find_element_by_xpath("//*[text()='Manual']")
manual_nav_link.click()

automation_nav_link = self.driver.find_element_by_xpath("//*[text()='Automation']")
automation_nav_link.click()

webinar_footer_link = self.driver.find_element_by_xpath("//*[text()='Webinars']")
webinar_footer_link.click()

webinars_h2_title_text = self.driver.find_element_by_xpath("//h2[1]").text

print("The title of the first h2 is: " + webinars_h2_title_text)

def tearDown(self):
self.driver.quit()

if __name__ == '__main__':
unittest.main()
warning

Before you begin, make sure to install Python bindings for Selenium.

In your terminal or command line, run the command:

pip install -U selenium

Alternatively, if you don't have pip installed on your machine, you can download the bindings and run the setup file.

Download the file, unzip it, open your terminal or command line in the unzipped folder and run the command:

python setup.py install