Selenium's python source code interpretation - WebDriverWait

1. Display waiting

The so-called display waiting is to set the waiting time for a specific element. If the element is found within the specified time, the relevant operation will be performed. If the element is not found within the specified time, an exception will be thrown.

PS: Pay attention to the difference between display waiting and incognito wait. Incognito wait is to set the loading time for all elements in the page.

Second, the WebDriverWait class

Displaying waiting in the selenium framework is achieved through WebDriverWait

WebDriverWait location: In the wait.py file of the support package, selenium\webdriver\support\wait.py

Constitute the function:

class WebDriverWait(object):
    def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
        pass

Interpretation of constructor parameters:

driver: webdriver instance, can be Ie, Firefox, Chrome or Remote
timeout: the maximum timeout when searching for a specific element, in seconds
poll_frequency: keyword parameter, the frequency of finding elements, that is, how often the method is called back, the default is 0.5 second
ignored_exceptions: keyword parameter, which exceptions are ignored in the callback method process, the default is NoSuchElementException, if you need to ignore multiple exceptions, you can pass in the parameters in the form of iterable, such as the tuple 

instance method:
def until(self, method, message=''):
        """Calls the method provided with the driver as an argument until the \
        return value is not False."""

def until_not(self, method, message=''):
        """Calls the method provided with the driver as an argument until the \
        return value is False."""

Interpretation of the method:

until: the method of calling the method in a loop until the method returns true() non-false, the return result: the result returned by the method method
until_not: The method of calling the method in a loop until the method returns false. 
In these two methods, the parameter method is a required parameter, and a functhon object is passed in (note that it is a function object without parentheses), the method object's The parameter is driver
def until(self, method, message=''):
    screen = None
    stacktrace = None

    #Set the timeout time 
    end_time = time.time() + self._timeout
     while True: #Call the method method in a loop 
        try :
             #Execute the incoming method method, and the parameter is driver 
            value = method(self._driver)
             if value : #If The result of method method execution is non-false, then stop the loop 
                return value
         except self._ignored_exceptions as exc:
            screen = getattr(exc, 'screen', None)
            stacktrace = getattr(exc, 'stacktrace', None)
        time.sleep(self._poll)
        if time.time() > end_time: #If the loop time exceeds the set timeout, exit needs to 
            break 
    raise TimeoutException(message, screen, stacktrace)

for example:

from selenium.webdriver.support.ui import WebDriverWait #Import WebDriverWait 
#method : lambda anonymous function whose parameter is driver 
#Function : find the element whose id is someId #Return 
value : WebElement object with x.find_element_by_id("someId") 
element = WebDriverWait (driver, 10).until( lambda x: x.find_element_by_id( " someId " ))

Summarize:

until / until_not方法

Parameters: method object (note that it is an object, not a calling method), you can pass in a lambda anonymous function

The parameter of the method method is driver

 

Guess you like

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