Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/safari/webdriver.py
4048 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
from selenium.common.exceptions import WebDriverException
19
from selenium.webdriver.common.driver_finder import DriverFinder
20
from selenium.webdriver.common.webdriver import LocalWebDriver
21
from selenium.webdriver.safari.options import Options
22
from selenium.webdriver.safari.remote_connection import SafariRemoteConnection
23
from selenium.webdriver.safari.service import Service
24
25
26
class WebDriver(LocalWebDriver):
27
"""Controls the SafariDriver and allows you to drive the browser."""
28
29
def __init__(
30
self,
31
options: Options | None = None,
32
service: Service | None = None,
33
keep_alive: bool = True,
34
) -> None:
35
"""Create a new Safari driver instance and launch or find a running safaridriver service.
36
37
Args:
38
options: Instance of Options.
39
service: Service object for handling the browser driver if you need to pass extra details.
40
keep_alive: Whether to configure SafariRemoteConnection to use HTTP keep-alive.
41
"""
42
self.service = service if service else Service()
43
self.options = options if options else Options()
44
45
self.service.path = self.service.env_path() or DriverFinder(self.service, self.options).get_driver_path()
46
47
if not self.service.reuse_service:
48
self.service.start()
49
50
executor = SafariRemoteConnection(
51
remote_server_addr=self.service.service_url,
52
keep_alive=keep_alive,
53
ignore_proxy=self.options._ignore_local_proxy,
54
)
55
56
try:
57
super().__init__(command_executor=executor, options=self.options)
58
except Exception:
59
self.quit()
60
raise
61
62
def quit(self):
63
"""Closes the browser and shuts down the SafariDriver executable."""
64
try:
65
super().quit()
66
except Exception:
67
# We don't care about the message because something probably has gone wrong
68
pass
69
finally:
70
if not self.service.reuse_service:
71
self.service.stop()
72
73
# safaridriver extension commands. The canonical command support matrix is here:
74
# https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/WebDriverEndpointDoc/Commands/Commands.html
75
76
# First available in Safari 11.1 and Safari Technology Preview 41.
77
def set_permission(self, permission, value):
78
if not isinstance(value, bool):
79
raise WebDriverException("Value of a session permission must be set to True or False.")
80
81
payload = {permission: value}
82
self.execute("SET_PERMISSIONS", {"permissions": payload})
83
84
# First available in Safari 11.1 and Safari Technology Preview 41.
85
def get_permission(self, permission):
86
payload = self.execute("GET_PERMISSIONS")["value"]
87
permissions = payload["permissions"]
88
if not permissions:
89
return None
90
91
if permission not in permissions:
92
return None
93
94
value = permissions[permission]
95
if not isinstance(value, bool):
96
return None
97
98
return value
99
100
# First available in Safari 11.1 and Safari Technology Preview 42.
101
def debug(self):
102
self.execute("ATTACH_DEBUGGER")
103
self.execute_script("debugger;")
104
105