GUI Elements Locators Flashcards
find_element_ (return a single WebElement) or find_elements_ (return a list of WebElements) methods:
by_id (id_) by_class_name (name) by_tag_name (name) by_xpath (xpath) by_css_selector (css_selector)
W3C DOM Definition
A platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document.
What it is included in the DOM definition of web elements?
- HTML elements as objects
- The properties of all HTML elements
- The methods that can be used to access all HTML elements
- The events that affect all HTML elements
CODE: Locate by ID
element = driver.find_element_by_id(‘the_id’)
CODE: Locate by Class
element = driver.find_element_by_class_name(‘the_class’)
CODE: Locate by Tag
element = driver.find_element_by_tag_name(‘h2’)
Two methods to identify the WebElement by the link text (an anchor tag which contains text that will be highlighted for a user to click on):
element = driver.find_element_by_link_text(‘Click the link’)
element = driver.find_element_by_partial_link_text(‘Click’)
CODE: Using XPath Location
element = driver.find_element_by_xpath(‘//form[@id=’sample’]/input[2]’)
CODE: Identifying WebElement via CSS Selector
driver.find_element_by_css_selector(‘p.paragraph’)
predefined classes that do not simply determine if an element exists, but also check for a specific state for that element to be in.
expected_conditions module from selenium.webdriver.support
For test automation, what are the reasons do we have to know more information (its current visibility, whether it is enabled or not, or whether it is selected or not) about the elements in a webpage?
- Ensure the state is as expected at a given point in the test
- Make sure a control is in a state that it can be manipulated as needed in the test case (i.e., enabled)
- Make sure that after the control was manipulated, it is now in the expected state
- Make sure that the expected results are correct after a test is run
get_attribute()
Gets property. If no property by that name, gets attribute of that name. If neither, returns None
get_property()
Gets property.
is_displayed()
Returns true if visible to user
is_enabled()
returns true if element is enabled
is_selected()
Returns X,Y location on the renderable canvas
size
Returns the Height and Width of the element
tag_name
Returns the tag_name of the element
text
Returns the text associated with the element
CODE: clear input control
Element.clear()
CODE: Type into an input control
Element.send_keys(“this is the typed text”)
CODE: wait to ensure that an element it is ready to be clicked
Driver.support.expected_conditions.element_to_be_clickable(locator)
CODE: Click on WebElement
Element.click()
Selection options in Select Control:
- Search through the HTML to find the item desired and then click on it
- Select by an item value (select_by_value(value))
- Select all items that display matching text (select_by_visible_text(text))
- Select an item by index (select_by_index(index))
Deselection options in Select Control:
- Deselect all items (deselect_all())
- Deselect by index (deselect_by_index(index))
- Deselect by value (deselect_by_value(value))
- Deselect by visible text (deselect_by_visible_text(text))
options that allows to see what has been selected in a Select Control:
- Return a list of all selected items (all_selected_options)
- Return the first selected (or only if a single select control) (first_selected_option)
- See a list of all the options in the list (options)
What is a dialog box:
a box that pops up over a browser window and does not allow access to the underlying window until it has been handled
What are user prompts?
modal windows requiring users to interact with them before the user can continue to interact with the controls in the browser window itself.
Three different alert-type dialogs defined by W3C:
- Alert
- Confirm
- Prompt
When the “unexpected alert open” error could be returned by a script action?
When the script tries to ignore a prompt and continue sending commands to the browser window itself
Hows the alert dialog box is often used?
to make sure that the user is made aware of some meaningful information.
Why alert boxes require special handling?
alert dialog boxes are not actually a part of the web page
CODE: Create an Alert Object
driver.switch_to.alert
What are the two ways in that Alert dialogs may be closed?
# first Create an Alert Object # then
A)
alert.accept()
# B)
alert.dismiss()