Article Outline
Python web automation, selenium example 'SendKeys'
Modules used in program:
import time
import unittest
SendKeys
Python selenium example: SendKeys
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import unittest
import time
class sendKeys(unittest.TestCase):
def setUp(self):
global driver
driver = webdriver.Firefox()
driver.get("http://travelingtony.weebly.com")
driver.maximize_window()
def test_sendKeys(self):
#Locators
searchFieldName = "q"
turtlePictureLocator = "//div[@title='Leatherback Turtle Picture']"
searchFieldElement = WebDriverWait(driver, 10).\
until(lambda driver: driver.find_element_by_name(searchFieldName))
actions = ActionChains(driver) #Used to create action chains
# actions.send_keys_to_element(searchFieldElement, "Leatherback")
# actions.send_keys_to_element(searchFieldElement, Keys.ENTER)
# actions.perform()
#Alternate method - requires more actions but you have the option of picking either
actions.click()
actions.send_keys("Leatherback")
actions.send_keys(Keys.ENTER)
actions.perform()
WebDriverWait(driver, 10).\
until(lambda driver: driver.find_element_by_xpath(turtlePictureLocator))
def tearDown(self):
driver.quit()
if __name__ == "__main__":
unittest.main()
Useful links
- Learn Python: https://pythonbasics.org/
- More Python: https://pythonprogramminglanguage.com