Path: blob/trunk/py/selenium/webdriver/common/timeouts.py
1864 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617from typing import TYPE_CHECKING1819if TYPE_CHECKING:20from typing import TypedDict2122class JSONTimeouts(TypedDict, total=False):23implicit: int24pageLoad: int25script: int2627else:28JSONTimeouts = dict[str, int]293031class _TimeoutsDescriptor:32"""Get or set the value of the attributes listed below.3334_implicit_wait _page_load _script3536This does not set the value on the remote end.37"""3839def __init__(self, name):40self.name = name4142def __get__(self, obj, cls) -> float:43return getattr(obj, self.name) / 10004445def __set__(self, obj, value) -> None:46converted_value = getattr(obj, "_convert")(value)47setattr(obj, self.name, converted_value)484950class Timeouts:51def __init__(self, implicit_wait: float = 0, page_load: float = 0, script: float = 0) -> None:52"""Create a new Timeouts object.5354This implements https://w3c.github.io/webdriver/#timeouts.5556:Args:57- implicit_wait - Either an int or a float. Set how many58seconds to wait when searching for elements before59throwing an error.60- page_load - Either an int or a float. Set how many seconds61to wait for a page load to complete before throwing62an error.63- script - Either an int or a float. Set how many seconds to64wait for an asynchronous script to finish execution65before throwing an error.66"""6768self._implicit_wait = self._convert(implicit_wait)69self._page_load = self._convert(page_load)70self._script = self._convert(script)7172# Creating descriptor objects73implicit_wait = _TimeoutsDescriptor("_implicit_wait")74"""Get or set how many seconds to wait when searching for elements.7576This does not set the value on the remote end.7778Usage:79------80- Get81- `self.implicit_wait`82- Set83- `self.implicit_wait` = `value`8485Parameters:86-----------87`value`: `float`88"""8990page_load = _TimeoutsDescriptor("_page_load")91"""Get or set how many seconds to wait for the page to load.9293This does not set the value on the remote end.9495Usage:96------97- Get98- `self.page_load`99- Set100- `self.page_load` = `value`101102Parameters:103-----------104`value`: `float`105"""106107script = _TimeoutsDescriptor("_script")108"""Get or set how many seconds to wait for an asynchronous script to finish109execution.110111This does not set the value on the remote end.112113Usage:114------115- Get116- `self.script`117- Set118- `self.script` = `value`119120Parameters:121-----------122`value`: `float`123"""124125def _convert(self, timeout: float) -> int:126if isinstance(timeout, (int, float)):127return int(float(timeout) * 1000)128raise TypeError("Timeouts can only be an int or a float")129130def _to_json(self) -> JSONTimeouts:131timeouts: JSONTimeouts = {}132if self._implicit_wait:133timeouts["implicit"] = self._implicit_wait134if self._page_load:135timeouts["pageLoad"] = self._page_load136if self._script:137timeouts["script"] = self._script138139return timeouts140141142