Selenium has three waiting modes (forced waiting, implicit waiting and display waiting)

1. Forced wait (sleep)

from time import sleep
sleep(3)  # Forced wait for 3 seconds

Disadvantages: because the speed of Web loading depends on the hardware, network speed, server response time and other factors. If the waiting time is too long, it is easy to waste time. If the waiting time is too short, it may result in an error when the web has not loaded the element s that need to be located.
Since the waiting time cannot be determined, using too many sleep will affect the running speed and greatly reduce the efficiency. Therefore, it is recommended to minimize the use of forced waiting in the test.

2. Implicitly_wait

# Implicit wait 10s

Introduction: implicit waiting is global. It sets the waiting time for all elements, such as 10 seconds. If it occurs within 10 seconds, it will continue to go down, otherwise an exception will be thrown. It can be understood that within 10 seconds, keep refreshing to see whether the element is loaded.
Usage scenario: implicit waiting only needs to be declared once, which is generally declared after opening the browser. The declaration is valid for the entire drvier life cycle, and there is no need to repeat the declaration later. There is a problem with implicit waiting, that is, the program will wait until the whole page is loaded, that is, generally, you will not execute the next step until you see that the small circle in the browser tab bar is no longer turned, but sometimes the elements you want on the page are already loaded, but because some js and other things are particularly slow, You still have to wait until the page is complete to proceed to the next step.

3. Show expected_conditions

Introduction: display waiting is to set a waiting time for an element separately, such as 5 seconds. Check whether it appears every 0.5 seconds. If it appears at any time before 5 seconds, continue to go down. Generally, it needs to cooperate with the until() and until() of this class_ The not () method is used together until the set maximum time is exceeded, and then a timeout error TimeoutException is thrown. The following are some of the most commonly used methods:

1. Judge whether the element is visible: visibility_of_element_located(locator) (visible means element is not hidden, and element width and height are not equal to 0)

from import expected_conditions as EC
from import WebDriverWait
from import By
# Examples
target = EC.visibility_of_element_located(By.ID,'user')

# Use with until() (wait for element to be visible)
# 5 indicates the maximum timeout time, which is in seconds by default; 1 indicates the interval step of detection. During the waiting period, call until or until every certain time (0.5 seconds by default)_ Not until it returns True or False
WebDriverWait(driver, 5, 1).until(EC.visibility_of_element_located(By.ID,'user'))
# Cooperate until_not() use (wait element is not visible)
WebDriverWait(driver, 5, 1).until_not(EC.visibility_of_element_located(By.ID,'user'))

# Encapsulated as a function in a class
    def wait_eleLocated(self, loc, timeout=30, poll_frequency=0.5, model=None):
        :param loc:Element location expression;Tuple type,Expression(Element location type,Element positioning method),Example:(By.ID, "kw")
        :param timeout:Timeout
        :param poll_frequency:Polling frequency
        :param model:When waiting fails,Screenshot operation,Function labels to be expressed in picture files
        """'wait for"{model}"element,Positioning mode:{loc}')
            start =
            WebDriverWait(self.driver, timeout, poll_frequency).until(EC.visibility_of_element_located(loc))
            end =
  'wait for"{model}"duration:{end - start}')
        except TimeoutException:
            self.logger.exception(f'wait for"{model}"Element failed,Positioning mode:{loc}')
            # screenshot
            self.save_webImgs(f"Wait element[{model}]An exception occurred")

2. Judge whether an element is loaded into the dom tree: presence_of_element_located(locator) (does not mean that the element must be visible)

from import expected_conditions as EC
from import By

target = EC.presence_of_element_located(By.ID,'user')

3. Judge whether an element is visible and click: element_to_be_clickable(locator)

from import expected_conditions as EC
from import By

target = EC.element_to_be_clickable(By.ID,'user')

4. Judge whether an element is selected: element_to_be_selected(element) (generally used in drop-down list)

from import expected_conditions as EC
from import By

element = driver.find_element_by_class_name('selector')

The following is the supporting materials. For friends who do [software testing], it should be the most comprehensive and complete war preparation warehouse. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you!

Finally, it can be in the official account: programmer Hao! Get a 216 page interview document of Software Test Engineer for free. And the corresponding video learning tutorials for free!, It includes basic knowledge, Linux essentials, Shell, Internet program principles, Mysql database, special topics of packet capture tools, interface test tools, test advanced Python programming, Web automation test, APP automation test, interface automation test, advanced continuous integration of test, test architecture, development test framework, performance test, security test, etc.

If my blog is helpful to you and you like my blog content, please click "like", "comment" and "collect" for three times! Friends who like software testing can join our testing technology exchange group: 779450660 (there are various software testing resources and technical discussions)

Keywords: Python Selenium software testing Testing

Added by r-it on Mon, 14 Feb 2022 13:39:04 +0200