Path: blob/trunk/py/selenium/webdriver/common/timeouts.py
4014 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.5556Args:57implicit_wait: Number of seconds to wait when searching for elements58before throwing an error.59page_load: Number of seconds to wait for a page load to complete60before throwing an error.61script: Number of seconds to wait for an asynchronous script to62finish execution before throwing an error.63"""64self._implicit_wait = self._convert(implicit_wait)65self._page_load = self._convert(page_load)66self._script = self._convert(script)6768# Creating descriptor objects69implicit_wait = _TimeoutsDescriptor("_implicit_wait")70"""Number of seconds to wait when searching for elements.7172Note: This does not set the value on the remote end.73"""7475page_load = _TimeoutsDescriptor("_page_load")76"""Number of seconds to wait for the page to load.7778Note: This does not set the value on the remote end.79"""8081script = _TimeoutsDescriptor("_script")82"""Number of seconds to wait for an asynchronous script to finish execution.8384Note: This does not set the value on the remote end.85"""8687def _convert(self, timeout: float) -> int:88if isinstance(timeout, (int, float)):89return int(float(timeout) * 1000)90raise TypeError("Timeouts can only be an int or a float")9192def _to_json(self) -> JSONTimeouts:93timeouts: JSONTimeouts = {}94if self._implicit_wait:95timeouts["implicit"] = self._implicit_wait96if self._page_load:97timeouts["pageLoad"] = self._page_load98if self._script:99timeouts["script"] = self._script100101return timeouts102103104