Selenium - three ways to wait

In UI automation testing, it is inevitable that the environment is unstable and the network is slow. If nothing is done at this time, the code will report an error because the element is not found. At this time, we need to use wait, and in Selenium, we can use a total of three kinds of waiting, each of which has its own advantages or disadvantages, if you choose the optimal waiting method.

time (fixed wait)

In the process of developing the automation framework, it is most taboo to use the time sleep method of python's own module to wait. Although the waiting time can be customized, when the network conditions are good, it will continue to wait according to the preset time, resulting in the entire project. Automation time is extended indefinitely. Not recommended for use. (Note: During the script debugging process, it can still be used, which is convenient and fast)

implicitly_wait (implicit wait)

Implicit waiting actually sets a maximum waiting time. If the web page is loaded within the specified time, execute the next step, otherwise wait until the time ends, and then execute the next step. There will be a pit in such implicit waiting. We all know that js is generally loaded at the end of our body. In fact, all the elements on the page have been loaded, but we are still waiting for the end of loading of all pages. Implicit wait works for the entire driver cycle, and it can be set once at the beginning. Don't use it as a fixed wait, use an implicit wait wherever you go.

WebDriverWait (display wait)

WebDriverWait is provided by selenium to get the display waiting module import path

from selenium.webdriver.support.wait import WebDriver

WebDriverWait parameter

driver: Pass in the WebDriver instance, which is the driver in our example above
timeout: timeout, the maximum time to wait
poll_frequency: The interval between calling the method in until or until_not, the default is 0.5 seconds
ignored_exceptions: ignored exceptions, if the exception in this tuple is thrown during the call to until or until_not,
Then do not interrupt the code and continue to wait. If an exception outside this tuple is thrown, the code is interrupted and an exception is thrown. By default only NoSuchElementException.

In this module, there are only two methods until and until_not

method: During the waiting period, call this incoming method at intervals until the return value is not False
message: If it times out, throw TimeoutException and pass the message to the exception

until

Continue to execute when an element appears or what conditions are met

until_not

Continue to execute when an element disappears or some conditions are not met

The method of the two methods must be an executable method with __call__. So we refer to a module provided by selenium

from selenium.webdriver.support import expected_conditions as Ec
When both ''' implicit waiting and explicit waiting exist, the timeout is the larger of the two ''' 
locator = (By.ID, ' kw ' )
driver.get(base_url)
 
WebDriverWait(driver, 10).until(EC.title_is(u " Baidu, you will know " ))
 ''' judgment title, return boolean '''
 
WebDriverWait(driver, 10).until(EC.title_contains(u " Baidu " ))
 ''' Judge the title and return the boolean value '''
 
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, ' kw ' )))
 ''' Determining whether an element is added to the dom tree does not mean that the element must be visible, if it is located just return WebElement '''
 
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, ' su ' )))
 ''' Determine whether an element has been added to the dom and is visible. Visible means that the element can be displayed with both width and height greater than 0 '''
 
WebDriverWait(driver, 10).until(EC.visibility_of(driver.find_element(by=By.ID,value= ' kw ' )))
 ''' Determine whether the element is visible, if it is visible, return this element '''
 
WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ' .mnav ' )))
 ''' Determine whether at least one element exists in the dom tree, if located, return the list '''
 
WebDriverWait(driver, 10).until(EC.visibility_of_any_elements_located((By.CSS_SELECTOR, ' .mnav ' )))
 ''' Determine whether at least one element is visible on the page, and return a list if it is located '''
 
WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element((By.XPATH, " //*[@id='u1']/a[8] " ),u ' set ' ))
 ''' Judge the specified Whether the element contains the expected string, returns boolean '''
 
WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element_value((By.CSS_SELECTOR, ' #su ' ),u ' Baidu Yiyi ' ))
 ''' Determines whether the attribute value of the specified element contains the expected string, and returns boolean '''
 
# WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it(locator)) 
''' Determine whether the frame can be switched in, if so, return True and switch in, otherwise return False ''' 
#Note that there is no one The frame can be switched into 
 
WebDriverWait(driver, 10).until(EC.invisibility_of_element_located((By.CSS_SELECTOR, ' #swfEveryCookieWrap ' )))
 ''' Determine whether an element exists in the dom or is not visible, if it is visible, return False, Invisible return this element ''' 
#Note #swfEveryCookieWrap is a hidden element in this page 
 
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, " //*[@id='u1' ]/a[8] " ))).click()
 '''Determine whether an element is visible and enabled, representing clickable '''
driver.find_element_by_xpath("//*[@id='wrapper']/div[6]/a[1]").click()
#WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='wrapper']/div[6]/a[1]"))).click()
 
# WebDriverWait(driver,10).until(EC.staleness_of(driver.find_element(By.ID,'su'))) 
''' Wait for an element to be removed from the dom tree ''' 
#No suitable ones were found here Example 
 
WebDriverWait(driver, 10).until(EC.element_to_be_selected(driver.find_element(By.XPATH, " //*[@id='nr']/option[1] " )))
 ''' Determine an element Whether it is selected, generally used in the drop-down list '''
 
WebDriverWait(driver, 10).until(EC.element_selection_state_to_be(driver.find_element(By.XPATH, " //*[@id='nr']/option[1] " ),True))
 ''' Determine a certain Whether the selected state of the element is as expected '''
 
WebDriverWait(driver, 10).until(EC.element_located_selection_state_to_be((By.XPATH, " //*[@id='nr']/option[1] " ),True))
 ''' Determine the selection of an element Is the state as expected ''' 
driver.find_element_by_xpath( " .//*[@id='gxszButton']/a[1] " ).click()
 
instance = WebDriverWait(driver,10 ).until(EC.alert_is_present())
 ''' Determine whether there is an alert on the page, if so, switch to the alert and return the content of the alert ''' 
print instance.text
instance.accept()
 
from selenium import webdriver
driver=webdriver.Chrome()
driver.get("http://ui.imdsx.cn/uitester/")
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as Ec
driver.maximize_window() #Enlarge the window 
driver.execute_script( ' window.scrollTo(0,0); ' )
e =WebDriverWait(driver,10).until(Ec.presence_of_element_located(( ' id ' , ' i1 ' ))) #When something appears, continue to execute e.send_keys 
(1111)
from selenium.webdriver.common.by import By
e=WebDriverWait(driver,10).until(Ec.presence_of_element_located((By.ID,'i1')))
WebDriverWait(driver, 10,1)10: Too long. 1: Step size, which means query every 1 second
WebDriverWait(driver, 10).until_not() and so on disappear to continue execution
Disadvantages of UI automation
 1. High maintenance cost (low output)
    a. The code does not do any decoupling
    b. There is no frame of mind, purely in the heap code (pageobject)
        According to each page of the project, the class is abstracted, and each function point is abstracted from the function of this class
    c. web development will change the html interface, or modify it
        The positioning method of css_selector minimizes the use of hierarchical positioning
2. Code loves to report errors
    WebDriverWait scans every few seconds, if it times out, it will report a timeout error
    The positioning method of css_selector minimizes the use of hierarchical positioning

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324867956&siteId=291194637