In this article, I want to introduce the common keys in Webdriver. The keys are used to simulate a user entering a key on the keyboard. Back space, enter, ALT, ESCAPE; these are the types of keys I am talking about. The place where this would be useful is, for example, the enter key could be used to submit a form in a web application. In the previous Hello World example, we entered the text 'Hello World!' into a search bar and then execute the search with a separate submit command. Another way we could have achieved the same result is to include the enter key at the end of the 'Hello World!' string.
As an example, below is a script to enter text into an input element to search the Yale University course catalog. The search string we will use is 'engl1*' which should return a list of all the one hundred level english courses.
#!/usr/bin/env python
from sys import argv
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
def yale_courses(s):
# start Firefox
driver = webdriver.Firefox()
# navigate to the Yale course catalog
driver.get('http://students.yale.edu/oci/search.jsp')
# find the Course number input page element
input = driver.find_element_by_name('CourseNumber')
# Enter the search string into the input element
input.send_keys(s)
# Take a screen shot
driver.save_screenshot('english_search')
# Execute search
input.send_keys(Keys.RETURN)
# Finished
driver.close()
if __name__=='__main__':
course = argv[1]
yale_courses(course)
No comments:
Post a Comment