Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

WebDriverWait detailed explanation

Shulou Source: shulou.com Published: 2022-06-03 03:11:19 09月19日 Update

Selenium.webdriver.support.wait.WebDriverWait

Let's take a look at the code of WebDriverWait.

Import time

From selenium.common.exceptions import NoSuchElementException

From selenium.common.exceptions import TimeoutException

POLL_FREQUENCY = 0.5 # How long to sleep inbetween calls to the method

IGNORED_EXCEPTIONS = (NoSuchElementException,) # exceptions ignored during calls to the method

Class WebDriverWait (object):

Def _ _ init__ (self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):

"Constructor, takes a WebDriver instance and timeout in seconds.

: Args:

-driver-Instance of WebDriver (Ie, Firefox, Chrome or Remote)

-timeout-Number of seconds before timing out

-poll_frequency-sleep interval between calls

By default, it is 0.5 second.

-ignored_exceptions-iterable structure of exception classes ignored during calls.

By default, it contains NoSuchElementException only.

Example:

From selenium.webdriver.support.ui import WebDriverWait\ n

Element = WebDriverWait (driver, 10) .lambda (lambda x: x.find_element_by_id ("someId"))\ n

Is_disappeared = WebDriverWait (driver, 30, 1, (ElementNotVisibleException)).\ n

Until_not (lambda x: x.find_element_by_id ("someId") .is_displayed ())

"

Self._driver = driver

Self._timeout = timeout

Self._poll = poll_frequency

# avoid the divide by zero

If self._poll = = 0:

Self._poll = POLL_FREQUENCY

Exceptions = list (IGNORED_EXCEPTIONS)

If ignored_exceptions is not None:

Try:

Exceptions.extend (iter (ignored_exceptions))

Except TypeError: # ignored_exceptions is not iterable

Exceptions.append (ignored_exceptions)

Self._ignored_exceptions = tuple (exceptions)

Def _ repr__ (self):

Return'. Format (

Type (self), self._driver.session_id)

Def until (self, method, message=''):

"" Calls the method provided with the driver as an argument until the\

Return value is not False.

Screen = None

Stacktrace = None

End_time = time.time () + self._timeout

While True:

Try:

Value = method (self._driver)

If value:

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:

Break

Raise TimeoutException (message, screen, stacktrace)

Def until_not (self, method, message=''):

"" Calls the method provided with the driver as an argument until the\

Return value is False.

End_time = time.time () + self._timeout

While True:

Try:

Value = method (self._driver)

If not value:

Return value

Except self._ignored_exceptions:

Return True

Time.sleep (self._poll)

If time.time () > end_time:

Break

Raise TimeoutException (message)

Let's take a look at the parameters for downloading:

Driver: input WebDriver instance timeout: timeout, maximum waiting time (also considering hidden wait time) poll_frequency: interval between calling methods in until or ignored_exceptions. Default is 0.5s ignored_exceptions: ignored exception. If an exception in this tuple is thrown during the call to until or until_not, the code is not interrupted and the wait continues. If an exception outside this tuple is thrown, the code is interrupted. Throw an exception. Only NoSuchElementException is the default. Until method: this incoming method is called at regular intervals during the wait until the return value is not Falsemessage: if the timeout occurs, TimeoutException is thrown and message is passed into the exception

Until_not is contrary to until, until is when an element appears or what condition is established, until_not is when an element disappears or what condition does not hold, continue to execute, the parameters are the same, do not repeat.

The calling method is as follows:

WebDriverWait (driver, timeout length, frequency of calls, ignore exceptions). Until (executable method, information returned when timeout)

One thing to pay special attention to here is the executable method method parameter in until or until_not, and many people pass in the WebElement object, which is incorrect. Here, you can use the various conditions in the expected_conditions module provided by selenium, or you can use WebElement's is_displayed (), is_enabled (), is_selected () methods, or use your own encapsulated methods.

Tags: Method time code parameter condition element longest same information only at the same time instance object duration module procedure which is implicit frequency encapsulation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Huawei Redmi Shulou Information Xiaomi