Essays and records to facilitate their access to fellow travelers.
#------------------------------------------------I ------------------------------------------- dividing line is a shame
Learning before selenium automation, it is best to learn HTML, CSS, JavaScript and other knowledge, help to understand the principles of operation and positioning elements. About python and selenium install their own search for other information,
Here do not introduced, all examples use python3.6 + selenium execution.
Today, most Web applications using AJAX technology. When the browser when the page loads, elements on the page could not be loaded at the same time, which gives the positioning of elements increases the difficulty. If because of delay in loading an element caused ElementNotVisibleException situation occurs, it will reduce the stability of automation scripts, we can set an element of instability to wait to improve such problems caused.
WebDriver provides two types of wait: the explicit and implicit waiting waiting.
#------------------------------------------------I ------------------------------------------- dividing line is a shame
Display wait
Explicit waiting for the WebDriver wait to continue if a condition exists, or else abandon timeout reached the maximum time an exception ( TimeoutException ).
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome() driver.get("http://www.baidu.com") element = WebDriverWait(driver,5,0.5).until( EC.presence_of_element_located((By.ID,"kw")) ) element.send_keys('selenium') driver.quit()
WebDriverWait class is WebDriver waiting methods provided. Within the set time, the default time to time be tested once the current page element exists, and if it exceeds the set time does not detect an exception is thrown. The following format:
WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_excptions=None)
Driver : Driver browser.
timeout : maximum timeout, default in seconds.
poll_frequency : detecting an interval (step). The default is 0.5s .
ignored_exceptions : abnormality information after a timeout, by default, throw NoSunchElementException exception.
WebDriverWait () generally made until or () until_not method with the use of the following is until and until_not () described methods.
until (method, message = '' ) # calls the driver to provide the method as a parameter, until the return value is True .
until_not (method, message = '' ) # calls the driver to provide the method as a parameter, until the return value is False .
In the present embodiment, by as keyword expected_conditions rename EC , and calls presence_of_element_located () method for determining whether the element is present.
expected_conditions method contemplated classes provide conditions determined in the following table:
method |
Explanation |
title_is |
Determine whether the current page title equal to the expected |
title_contains |
Determine whether the current page title contains the expected string |
presence_of_element_located |
Determine whether the element is loaded DOM tree, does not mean that certain elements visible |
visibility_of_element_located |
Determining when the element is visible (visible on behalf of a non-hidden elements, and the width and height of the element are not equal to 0 ) |
visibility_o |
And a method for the same action, but on a positioning method parameter, which received the parameter elements positioned |
presence_of_all_elements_located |
Determining whether there is at least one element is present in the DOM tree. For example, in the pages of n elements of class to "wp", as long as there is an existing returns True |
text_to_be_present_in_element |
Determining an element of text contains the expected string |
text_to_be_present_in_element_value |
An element of judgment valueshuxing contain the expected string |
frame_to_be_available_and_switch_to_it |
Determining whether to switch into the form, if possible, return True and the switch in, otherwise False |
invisible_of_element_located |
Determine whether an element not present in the DOM tree or invisible |
element_to_he_clickable |
Determine whether the element is visible and clickable |
staleness_of |
Wait until an element from the DOM removal of trees |
element_to_be_selected |
Determining whether an element is selected, generally used in the drop-down list |
element_selection_state_to_be |
Determine whether an element selected in line with expectations |
element_located_selection_state_to_be |
A method of acting on the same, only one parameter of the positioning method parameter, which parameter is received positional parameters |
alert_is_present |
To determine whether there is a page alert |
In addition expected_conditions to the rich condition judging contemplated provided methods can also be used in front learned is_displayed () method to determine whether the element is visible.
from selenium import webdriver from time import sleep,ctime driver = webdriver.Chrome() driver.get("http://www.baidu.com") print(ctime()) for i in range(5): try: el = driver.find_element_by_id('kw22') if el.is_displayed(): break sleep(1) except: print("time out") pass driver.close() print(ctime())
The results show:
Relatively speaking, this approach is more readily understood, by for loop 5 times, each cycle determining element is_displayed () whether the state is to true , as if to true , then break out of the loop; otherwise sleep (1) continue the cycle is determined, print "time out" information.