examples

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://localhost:8000')

assert 'Django' in browser.title


Functional tests should help you build an application with the right functionality, and guarantee you never accidentally break it (selenium webdriver).

Unit tests should help you write code that’s clean and bug free (unittest/django.test and pytest).

With unit testing:
To Run: python3 functional_tests.py

# functional_tests.py
from selenium import webdriver
import unittest

class NewVisitorTest(unittest.TestCase):

    def setUp(self):
        self.browser = webdriver.Firefox()

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

    def test_can_start_a_list_and_retrieve_it_later(self):
        self.browser.get('http://localhost:8000')

        self.assertIn('To-Do', self.browser.title)
        self.fail('Finish the test!')

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

Page Interaction

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Firefox()
driver.get("http://python.org")

# Find the search element
search = driver.find_element_by_name('q')
# Delete any existing text that is there
search.clear()
# Enter text into the search box
search.send_keys("pycon")
search.send_keys(Keys.RETURN)
time.sleep(4)

driver.close()

Filling Forms

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time

driver = webdriver.Firefox()
driver.get("http://url to your form")

select = Select(driver.find_element_by_name('name of the element'))
select.select_by_index(4)
time.sleep(2)
select.select_by_visible_text("200")
time.sleep(2)
select.select_by_value("250")

# Show all available options on the select element
options = select.options
print(options)

submit_button = driver.find_element_by_name('continue')
submit_button.submit()
time.sleep(2)

driver.close()

Select Element: Live Example

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time

driver = webdriver.Firefox()
driver.get("https://wiki.python.org/moin/FrontPage")

select = Select(driver.find_element_by_xpath('//*/form/div/select'))
time.sleep(2)
select.select_by_visible_text("Raw Text")
time.sleep(2)

driver.close()