Chapter3 Working with Elements
3.1 Automating textboxes, text areas, and buttons
3.1.1 Clearing text from textbox and text-area elements
works only on a textbox and text area
area.clear()
3.1.2 Entering text in textbox and text-area elements
can be used on any element that accepts values by typing on the element
area.send_keys("Selenium testing tools cook book")
we can also simulate pressing non-text keys using the send_keys() method by using Keys
area.send_keys("123" + webdriver.common.keys.Keys.TAB)
list of Keys: https://selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.keys.html
3.1.3 Submitting forms
area.submit()
3.1.4 Performing a click on a button element
button.click()
Clicking on an invisible element will result in a ElementNotVisibleException exception
3.2 Checking an element's text
button = driver.find_element_by_id("web-nav-app-download-btn").find_element_by_tag_name("span")
print(button.text)
3.3 Checking an element's attribute and CSS values
button.get_attribute("class")
button.value_of_css_property("width")
3.4 Automating dropdowns and lists
remember
from selenium.webdriver.support.select import Select
at the head place
lang = driver.find_element_by_tag_name("select")
# returns a list of all options belonging to this select tag
print(len(Select(lang).options))
Select(lang).select_by_visible_text("JavaScript")
Select(lang).select_by_value("JavaScript")
Select(lang).select_by_index(0)
3.5 Checking options in the Select element
remember to import unittest because of assert method
import unittest
lang = driver.find_element_by_tag_name("select")
expectedOptions=['html','css','JavaScript','php']
actualOptions = []
for option in Select(lang).options:
actualOptions.append(option.text)
print(actualOptions)
print(expectedOptions)
tc = unittest.TestCase()
tc.assertListEqual(actualOptions,expectedOptions)
3.6 Checking selected options in dropdowns and lists
Select(lang).select_by_visible_text("html")
Select(lang).select_by_visible_text("css")
Select(lang).select_by_visible_text("JavaScript")
Select(lang).select_by_visible_text("php")
Select(lang).deselect_by_visible_text("php")
It seems that only one option is selected. When I select "css", "html" is deselected
only multi-select box will support selecting more than 1 option...
3.7 Automating radio buttons and radio groups
tc = unittest.TestCase()
apple = driver.find_element_by_xpath("//input[@value='apple']")
print(apple.get_attribute("value"))
if not apple.is_selected():
apple.click()
tc.assertTrue(apple.is_selected())
fruits = driver.find_elements_by_tag_name("input")
print(len(fruits))
for fruit in fruits:
print(fruit.get_attribute("value"))
3.8 Automating checkboxes
tc = unittest.TestCase()
fruits = driver.find_elements_by_tag_name("input")
for fruit in fruits:
if not fruit.is_selected():
fruit.click()
tc.assertTrue(fruit.is_selected())
print(fruit.get_attribute("value"))
print(len(fruits))
3.9 Working with WebTables
rows = driver.find_elements_by_tag_name("tr")
print(len(rows))
for row in rows:
cols = row.find_elements_by_tag_name("td")
print(len(cols))
for col in cols:
print(col.text + "\t")