| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
from selenium.common.exceptions import WebDriverException |
| 19 |
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver |
| 20 |
|
| 21 |
from ..common.driver_finder import DriverFinder |
| 22 |
from .options import Options |
| 23 |
from .remote_connection import SafariRemoteConnection |
| 24 |
from .service import Service |
| 25 |
|
| 26 |
|
| 27 |
class WebDriver(RemoteWebDriver): |
| 28 |
"""Controls the SafariDriver and allows you to drive the browser.""" |
| 29 |
|
| 30 |
def __init__( |
| 31 |
self, |
| 32 |
keep_alive=True, |
| 33 |
options: Options = None, |
| 34 |
service: Service = None, |
| 35 |
) -> None: |
| 36 |
"""Creates a new Safari driver instance and launches or finds a running |
| 37 |
safaridriver service. |
| 38 |
|
| 39 |
:Args: |
| 40 |
- keep_alive - Whether to configure SafariRemoteConnection to use |
| 41 |
HTTP keep-alive. Defaults to True. |
| 42 |
- options - Instance of ``options.Options``. |
| 43 |
- service - Service object for handling the browser driver if you need to pass extra details |
| 44 |
""" |
| 45 |
self.service = service if service else Service() |
| 46 |
options = options if options else Options() |
| 47 |
|
| 48 |
self.service.path = DriverFinder(self.service, options).get_driver_path() |
| 49 |
|
| 50 |
if not self.service.reuse_service: |
| 51 |
self.service.start() |
| 52 |
|
| 53 |
executor = SafariRemoteConnection( |
| 54 |
remote_server_addr=self.service.service_url, |
| 55 |
keep_alive=keep_alive, |
| 56 |
ignore_proxy=options._ignore_local_proxy, |
| 57 |
) |
| 58 |
|
| 59 |
try: |
| 60 |
super().__init__(command_executor=executor, options=options) |
| 61 |
except Exception: |
| 62 |
self.quit() |
| 63 |
raise |
| 64 |
|
| 65 |
self._is_remote = False |
| 66 |
|
| 67 |
def quit(self): |
| 68 |
"""Closes the browser and shuts down the SafariDriver executable.""" |
| 69 |
try: |
| 70 |
super().quit() |
| 71 |
except Exception: |
| 72 |
|
| 73 |
pass |
| 74 |
finally: |
| 75 |
if not self.service.reuse_service: |
| 76 |
self.service.stop() |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
def set_permission(self, permission, value): |
| 83 |
if not isinstance(value, bool): |
| 84 |
raise WebDriverException("Value of a session permission must be set to True or False.") |
| 85 |
|
| 86 |
payload = {permission: value} |
| 87 |
self.execute("SET_PERMISSIONS", {"permissions": payload}) |
| 88 |
|
| 89 |
|
| 90 |
def get_permission(self, permission): |
| 91 |
payload = self.execute("GET_PERMISSIONS")["value"] |
| 92 |
permissions = payload["permissions"] |
| 93 |
if not permissions: |
| 94 |
return None |
| 95 |
|
| 96 |
if permission not in permissions: |
| 97 |
return None |
| 98 |
|
| 99 |
value = permissions[permission] |
| 100 |
if not isinstance(value, bool): |
| 101 |
return None |
| 102 |
|
| 103 |
return value |
| 104 |
|
| 105 |
|
| 106 |
def debug(self): |
| 107 |
self.execute("ATTACH_DEBUGGER") |
| 108 |
self.execute_script("debugger;") |