Selenium3 + Python3 automated test series of six - wait mode

wait

First, what wait, why wait

In doing automated testing, test case design, it is sometimes next operation will depend on the results of step or content to the next step after the success of the previous operation, etc.,

At this time, we need to wait to determine if the previous operation is completed, when you can be the next step. Otherwise, the last action if it takes a long time,

Has not been completed, go to the next step, then it will not produce target element, the element status is not correct, the data check result is incorrect and other abnormalities.

Selenium In waiting commonly divided into a display waiting WebDriverWait (), wait implicit implicitly_wait (), forced to wait for sleep () three.

Two, the Selenium three kinds of standby mode

time.sleep(n)

Wait n seconds force. Python function itself, contained in the packet time, the time required to import the package prior to use. In our previous example often use this kind of waiting, we can easily see the results of the script execution.

This method is very awkward wait, no matter how well the page is loaded, you must wait for n seconds, can not be said to be very intelligent of.

WebDriverWait(n)

The longest wait n seconds to check whether there is a need to locate elements within n seconds from time to time, if there is a premature end to wait. The method is also waiting WebDriver provided, also known as the dominant waiting,

This hidden waiting more intelligent than waiting a few, ignoring the load the entire page, as long as the required elements exist, waiting to end.

implicitly_wait(n) 

The longest wait for n seconds, if the page is loaded within n seconds to complete, the early termination of waiting. Wait method WebDriver provided, also known as hidden waiting, forced to wait more than a little smarter, but if the page itself contains a large video file and the like, 

Even if we need to locate elements in the beginning has been loaded up, but still have to wait for the end of all the files loaded, the script can continue to execute, still regarded as some drawbacks.

Third, forced to wait (sleep)

   For chestnuts, the following code to open the Baidu home automation, forced to wait five seconds, the browser address print, and close the browser.

from selenium.webdriver import Chrome
import time
driver = Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
driver.get('https://www.baidu.com/')

#强制等待5秒
time.sleep(5)

print(driver.current_url)
driver.quit()

Mandatory waiting time. Regardless of whether the browser loaded, must be forced to wait 5s time, too fierce, often not recommended to use this method to force wait ~

Fourth, shows the wait ( WebDriverWait )

  Import display wait: from selenium.webdriver.support.ui Import WebDriverWait 

  WebDriverWait class waiting methods provided by WebDirver. 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. Explicit waiting for the WebdDriver wait to continue if a condition exists, otherwise up

Throws a timeout exception (TimeoutException) when the maximum duration.

  Wait display format: WebDriverWait (Driver, timeout, poll_frequency = 0.5, ignored_exceptions = None)

  driver: browser driver  timeout: maximum timeout by default in seconds  poll_frequency: detecting an interval (step). The default is 0.5S

  ignored_exceptions: abnormality information after a timeout, by default NoSuchElementException throw an exception.

  WebDriverWait () by a mating generally an until () or until_not () method, described below is an until () and until_not () method.

  (1)until(method, message=‘’)

    This method provides the driver is called as a parameter, until the return value is True.

  (2)until_not(method, message=‘’)

    This method provides the driver is called as a parameter, until the return value is False.

  We give chestnut, as shown in the following code, before TimeoutException is thrown to wait 10 seconds or elements found within 10 seconds to find. Open the Baidu home page, to find out whether there is an element with ID kw.

from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
driver.get('https://www.baidu.com/')
#显式等待
element = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.ID, "kw")))

driver.quit()

  The selenium expected_conditions module collects a series of scene determination method, as described code EC.presence_of_all_elements_located method expected_conditions introduce several common scene determination method.

title_is: to determine whether the title of the current page is exactly equal to the expected

title_contains: to determine whether the current page title contains the expected string

presence_of_element_located: to determine whether an element is added to the dom tree, does not mean that certain elements visible

visibility_of_element_located: determining whether an element is representative of elements not hidden from invisible to visible, and the width and height of the elements is not equal to zero.

visibility_of: doing the same thing with the above method, only the above method to pass locator, this method is a direct pass to the element positioned just fine

presence_of_all_elements_located: determining whether there is at least one element is present in dom tree. For example, if there are elements of the class n pages are 'column-md-3', as long as there is an element exists, the method returns True

text_to_be_present_in_element: determining whether an element of the text contains the expected string

text_to_be_present_in_element_value: determine the value attribute contains an element of the expected string

frame_to_be_available_and_switch_to_it: to determine whether the frame can switch into it, if you can, and switch into returns True, otherwise it returns False

invisibility_of_element_located: to determine whether an element does not exist or is not visible in the dom tree

element_to_be_clickable: 判断某个元素中是否可见并且是enable的,这样的话才叫clickable

staleness_of: 等某个元素从dom树中移除,注意,这个方法也是返回True或False

element_to_be_selected: 判断某个元素是否被选中了,一般用在下拉列表

element_selection_state_to_be: 判断某个元素的选中状态是否符合预期

element_located_selection_state_to_be: 跟上面的方法作用一样,只是上面的方法传入定位到的element,而这个方法传入locator

alert_is_present: 判断页面上是否存在alert

五、隐式等待(implicitly_wait

隐式等待是指在尝试查找一个或多个元素(如果它们不是立即可用)时,WebDriver轮询DOM一段时间。默认设置为0。设置后,将为WebDriver对象实例的生命周期设置隐式等待

隐式等待举个栗子,打开百度页面,查找id为su的元素

from selenium.webdriver import Chrome
driver = Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
#隐式等待
driver.implicitly_wait(10)

driver.get("https://www.baidu.com/")
element = driver.find_element_by_id("su")

implicitly_wait()默认参数的单位为秒,本例中设置等待时长为10秒。首先这10秒并非一个固定的等待时间,它并不影响脚本的执行速度。其次,它并不针对页面上的某一元素进行等待。当脚本执行到某个元素定位时,如果元素可

以定位,则继续执行;如果元素定位不到,则它将以轮询的方式不断地判断元素是否被定位到。假设在第6秒定位到了元素则继续执行,若直到超出设置时长(10秒)还没有定位到元素,则抛出异常。

Guess you like

Origin www.cnblogs.com/wuweiblogs/p/10617365.html