selenium wait method

In the UI test automation, the environment will inevitably encounter the situation is unstable, slow network. When you think positioning is no problem, but the program has directly reported to the element is not visible, then you need to think about it because the program is running too fast or too slow page loads caused by the element is not visible, you must again wait until the element is visible and procedures continue to run. In Selenium, the wait three common methods have their advantages or disadvantages can try to choose the best way to wait for different situations in the understanding.


1. forced to wait (SLEEP)

The easiest method for setting wait is forced to wait, in fact, time.sleep () method, no matter what the circumstances, let the program run for a certain pause time, after time continues to run; the disadvantage is not smart, the set time is too short, the elements have not yet load out, it will still be an error; time is too long, it will be a waste of time, do not underestimate a few seconds each time, case more, a large amount of code, a number of seconds it will affect the overall speed up; so use this as little as possible.


#-*- coding:utf-8 -*-

import time

from datetime import datetime

print (datetime.now ()) # get the current time

time.sleep (10) # set the wait time 10s

print (datetime.now ()) # get the current time again

Code is very simple, mainly to get the current time after setting a wait time, and then set a time to wait for things in the back, waiting for this thing to see the current time after time. By the results we see good after serving only wait until the next step again.


2. recessive wait (implicitly_wait ())

Implicit wait actually set a maximum waiting time, if the page load completed within the stipulated time, then the next step, otherwise wait until the end of time, then the next step. Such implicit wait there will be a pit, we all know js usually placed at the end loading of our body, which is the actual elements on the page have been loaded, we are still waiting for the entire page has finished loading.


# -*- coding: utf-8 -*-  

from selenium import webdriver

import time

 

driver = webdriver.Chrome()  

driver.implicitly_wait (20) # recessive waiting longest 30 seconds,  

driver.get('https://www.baidu.com')

time.sleep(3)

driver.quit()

Implicit are waiting for the entire work cycle driver, at the beginning to set it once. Do not use as a fixed wait, where are about to implicitly wait.


3. Display standby mode (WebDriverWait)

It is clear to wait did not find elements within the prescribed time, the then throws Exception code examples are as follows:


# -*- coding: utf-8 -*-  

from selenium import webdriver  

from selenium.webdriver.support.wait import WebDriverWait  

from selenium.webdriver.support import expected_conditions as EC  

from selenium.webdriver.common.by import By  

driver = webdriver.Firefox()

driver.get('https://huilansame.github.io')  

WebDriverWait(driver,20,0.5).until(

    EC.presence_of_element_located((By.LINK_TEXT, 'CSDN')))  

print driver.find_element_by_link_text('CSDN').get_attribute('href')

driver.close()

WebDriverWait (driver, 20,0.5) .until (expected_conditions.presence_of_element_located (locator)), selenium in the wait module WebDriverWait () method, with or until until_not method, then assisted with some determination conditions, can constitute such a scenario: each of the number of seconds to view a locator element is visible, if visible stop waiting, if not visible continue to wait until exceeds the allotted time, reported a timeout exception; of course, can determine not visible within an element is in the specified time, etc. and other various scenes of it, you need to choose your own judgment based on the actual condition of the scene;


4. Summary

Implicit wait will wait for driver fully loaded (such as js, css, etc.); display only wait for the check to be loaded element exists; forced to wait well understood literally, not much to say. You can choose according to demand. Next Issue: selenium multi-window switching method.



Guess you like

Origin blog.51cto.com/13475644/2437222