initial commit
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
# Licensed to the Software Freedom Conservancy (SFC) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The SFC licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import importlib
|
||||
|
||||
_LAZY_SUBMODULES = ["firefox_profile", "options", "remote_connection", "service", "webdriver"]
|
||||
|
||||
|
||||
def __getattr__(name):
|
||||
if name in _LAZY_SUBMODULES:
|
||||
module = importlib.import_module(f".{name}", __name__)
|
||||
globals()[name] = module
|
||||
return module
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
|
||||
|
||||
def __dir__():
|
||||
return sorted(_LAZY_SUBMODULES)
|
||||
@@ -0,0 +1,26 @@
|
||||
# Licensed to the Software Freedom Conservancy (SFC) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The SFC licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License.
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
"""Type stub with lazy import mapping from __init__.py.
|
||||
|
||||
This stub file is necessary for type checkers and IDEs to automatically have
|
||||
visibility into lazy modules since they are not imported immediately at runtime.
|
||||
"""
|
||||
|
||||
from . import firefox_profile, options, remote_connection, service, webdriver
|
||||
|
||||
__all__ = ["firefox_profile", "options", "remote_connection", "service", "webdriver"]
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
+328
@@ -0,0 +1,328 @@
|
||||
# Licensed to the Software Freedom Conservancy (SFC) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The SFC licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import base64
|
||||
import copy
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
import warnings
|
||||
import zipfile
|
||||
from io import BytesIO
|
||||
from xml.dom import minidom
|
||||
|
||||
from typing_extensions import deprecated
|
||||
|
||||
from selenium.common.exceptions import WebDriverException
|
||||
|
||||
WEBDRIVER_PREFERENCES = "webdriver_prefs.json"
|
||||
|
||||
|
||||
@deprecated("Addons must be added after starting the session")
|
||||
class AddonFormatError(Exception):
|
||||
"""Exception for not well-formed add-on manifest files."""
|
||||
|
||||
|
||||
class FirefoxProfile:
|
||||
DEFAULT_PREFERENCES = None
|
||||
|
||||
def __init__(self, profile_directory=None):
|
||||
"""Initialises a new instance of a Firefox Profile.
|
||||
|
||||
Args:
|
||||
profile_directory: Directory of profile that you want to use. If a
|
||||
directory is passed in it will be cloned and the cloned directory
|
||||
will be used by the driver when instantiated.
|
||||
This defaults to None and will create a new
|
||||
directory when object is created.
|
||||
"""
|
||||
self._desired_preferences = {}
|
||||
if profile_directory:
|
||||
newprof = os.path.join(tempfile.mkdtemp(), "webdriver-py-profilecopy")
|
||||
shutil.copytree(
|
||||
profile_directory, newprof, ignore=shutil.ignore_patterns("parent.lock", "lock", ".parentlock")
|
||||
)
|
||||
self._profile_dir = newprof
|
||||
os.chmod(self._profile_dir, 0o755)
|
||||
else:
|
||||
self._profile_dir = tempfile.mkdtemp()
|
||||
if not FirefoxProfile.DEFAULT_PREFERENCES:
|
||||
with open(
|
||||
os.path.join(os.path.dirname(__file__), WEBDRIVER_PREFERENCES), encoding="utf-8"
|
||||
) as default_prefs:
|
||||
FirefoxProfile.DEFAULT_PREFERENCES = json.load(default_prefs)
|
||||
|
||||
self._desired_preferences = copy.deepcopy(FirefoxProfile.DEFAULT_PREFERENCES["mutable"])
|
||||
for key, value in FirefoxProfile.DEFAULT_PREFERENCES["frozen"].items():
|
||||
self._desired_preferences[key] = value
|
||||
|
||||
# Public Methods
|
||||
def set_preference(self, key, value):
|
||||
"""Sets the preference that we want in the profile."""
|
||||
self._desired_preferences[key] = value
|
||||
|
||||
@deprecated("Addons must be added after starting the session")
|
||||
def add_extension(self, extension=None):
|
||||
self._install_extension(extension)
|
||||
|
||||
def update_preferences(self):
|
||||
"""Writes the desired user prefs to disk."""
|
||||
user_prefs = os.path.join(self._profile_dir, "user.js")
|
||||
if os.path.isfile(user_prefs):
|
||||
os.chmod(user_prefs, 0o644)
|
||||
self._read_existing_userjs(user_prefs)
|
||||
with open(user_prefs, "w", encoding="utf-8") as f:
|
||||
for key, value in self._desired_preferences.items():
|
||||
f.write(f'user_pref("{key}", {json.dumps(value)});\n')
|
||||
|
||||
# Properties
|
||||
|
||||
@property
|
||||
def path(self):
|
||||
"""Gets the profile directory that is currently being used."""
|
||||
return self._profile_dir
|
||||
|
||||
@property
|
||||
@deprecated("The port is stored in the Service class")
|
||||
def port(self):
|
||||
"""Gets the port that WebDriver is working on."""
|
||||
return self._port
|
||||
|
||||
@port.setter
|
||||
@deprecated("The port is stored in the Service class")
|
||||
def port(self, port) -> None:
|
||||
"""Sets the port that WebDriver will be running on."""
|
||||
if not isinstance(port, int):
|
||||
raise WebDriverException("Port needs to be an integer")
|
||||
try:
|
||||
port = int(port)
|
||||
if port < 1 or port > 65535:
|
||||
raise WebDriverException("Port number must be in the range 1..65535")
|
||||
except (ValueError, TypeError):
|
||||
raise WebDriverException("Port needs to be an integer")
|
||||
self._port = port
|
||||
self.set_preference("webdriver_firefox_port", self._port)
|
||||
|
||||
@property
|
||||
@deprecated("Allowing untrusted certs is toggled in the Options class")
|
||||
def accept_untrusted_certs(self):
|
||||
return self._desired_preferences["webdriver_accept_untrusted_certs"]
|
||||
|
||||
@accept_untrusted_certs.setter
|
||||
@deprecated("Allowing untrusted certs is toggled in the Options class")
|
||||
def accept_untrusted_certs(self, value) -> None:
|
||||
if not isinstance(value, bool):
|
||||
raise WebDriverException("Please pass in a Boolean to this call")
|
||||
self.set_preference("webdriver_accept_untrusted_certs", value)
|
||||
|
||||
@property
|
||||
@deprecated("Allowing untrusted certs is toggled in the Options class")
|
||||
def assume_untrusted_cert_issuer(self):
|
||||
return self._desired_preferences["webdriver_assume_untrusted_issuer"]
|
||||
|
||||
@assume_untrusted_cert_issuer.setter
|
||||
@deprecated("Allowing untrusted certs is toggled in the Options class")
|
||||
def assume_untrusted_cert_issuer(self, value) -> None:
|
||||
if not isinstance(value, bool):
|
||||
raise WebDriverException("Please pass in a Boolean to this call")
|
||||
|
||||
self.set_preference("webdriver_assume_untrusted_issuer", value)
|
||||
|
||||
@property
|
||||
def encoded(self) -> str:
|
||||
"""Update preferences and create a zipped, base64-encoded profile directory string."""
|
||||
if self._desired_preferences:
|
||||
self.update_preferences()
|
||||
fp = BytesIO()
|
||||
with zipfile.ZipFile(fp, "w", zipfile.ZIP_DEFLATED, strict_timestamps=False) as zipped:
|
||||
path_root = len(self.path) + 1 # account for trailing slash
|
||||
for base, _, files in os.walk(self.path):
|
||||
for fyle in files:
|
||||
filename = os.path.join(base, fyle)
|
||||
zipped.write(filename, filename[path_root:])
|
||||
return base64.b64encode(fp.getvalue()).decode("UTF-8")
|
||||
|
||||
def _read_existing_userjs(self, userjs):
|
||||
"""Read existing preferences and add them to the desired preference dictionary."""
|
||||
pref_pattern = re.compile(r'user_pref\("(.*)",\s(.*)\)')
|
||||
with open(userjs, encoding="utf-8") as f:
|
||||
for usr in f:
|
||||
matches = pref_pattern.search(usr)
|
||||
try:
|
||||
self._desired_preferences[matches.group(1)] = json.loads(matches.group(2))
|
||||
except Exception:
|
||||
warnings.warn(
|
||||
f"(skipping) failed to json.loads existing preference: {matches.group(1) + matches.group(2)}"
|
||||
)
|
||||
|
||||
@deprecated("Addons must be added after starting the session")
|
||||
def _install_extension(self, addon, unpack=True):
|
||||
"""Install addon from a filepath, URL, or directory of addons in the profile.
|
||||
|
||||
Args:
|
||||
addon: url, absolute path to .xpi, or directory of addons
|
||||
unpack: whether to unpack unless specified otherwise in the install.rdf
|
||||
"""
|
||||
tmpdir = None
|
||||
xpifile = None
|
||||
if addon.endswith(".xpi"):
|
||||
tmpdir = tempfile.mkdtemp(suffix="." + os.path.split(addon)[-1])
|
||||
compressed_file = zipfile.ZipFile(addon, "r")
|
||||
for name in compressed_file.namelist():
|
||||
if name.endswith("/"):
|
||||
if not os.path.isdir(os.path.join(tmpdir, name)):
|
||||
os.makedirs(os.path.join(tmpdir, name))
|
||||
else:
|
||||
if not os.path.isdir(os.path.dirname(os.path.join(tmpdir, name))):
|
||||
os.makedirs(os.path.dirname(os.path.join(tmpdir, name)))
|
||||
data = compressed_file.read(name)
|
||||
with open(os.path.join(tmpdir, name), "wb") as f:
|
||||
f.write(data)
|
||||
xpifile = addon
|
||||
addon = tmpdir
|
||||
|
||||
# determine the addon id
|
||||
addon_details = self._addon_details(addon)
|
||||
addon_id = addon_details.get("id")
|
||||
assert addon_id, f"The addon id could not be found: {addon}"
|
||||
|
||||
# copy the addon to the profile
|
||||
extensions_dir = os.path.join(self._profile_dir, "extensions")
|
||||
addon_path = os.path.join(extensions_dir, addon_id)
|
||||
if not unpack and not addon_details["unpack"] and xpifile:
|
||||
if not os.path.exists(extensions_dir):
|
||||
os.makedirs(extensions_dir)
|
||||
os.chmod(extensions_dir, 0o755)
|
||||
shutil.copy(xpifile, addon_path + ".xpi")
|
||||
else:
|
||||
if not os.path.exists(addon_path):
|
||||
shutil.copytree(addon, addon_path, symlinks=True)
|
||||
|
||||
# remove the temporary directory, if any
|
||||
if tmpdir:
|
||||
shutil.rmtree(tmpdir)
|
||||
|
||||
@deprecated("Addons must be added after starting the session")
|
||||
def _addon_details(self, addon_path):
|
||||
"""Returns a dictionary of details about the addon.
|
||||
|
||||
Args:
|
||||
addon_path: path to the add-on directory or XPI
|
||||
|
||||
Returns:
|
||||
A dictionary containing:
|
||||
|
||||
{
|
||||
"id": "rainbow@colors.org", # id of the addon
|
||||
"version": "1.4", # version of the addon
|
||||
"name": "Rainbow", # name of the addon
|
||||
"unpack": False,
|
||||
} # whether to unpack the addon
|
||||
"""
|
||||
details = {"id": None, "unpack": False, "name": None, "version": None}
|
||||
|
||||
def get_namespace_id(doc, url):
|
||||
attributes = doc.documentElement.attributes
|
||||
namespace = ""
|
||||
for i in range(attributes.length):
|
||||
if attributes.item(i).value == url:
|
||||
if ":" in attributes.item(i).name:
|
||||
# If the namespace is not the default one remove 'xlmns:'
|
||||
namespace = attributes.item(i).name.split(":")[1] + ":"
|
||||
break
|
||||
return namespace
|
||||
|
||||
def get_text(element):
|
||||
"""Retrieve the text value of a given node."""
|
||||
rc = []
|
||||
for node in element.childNodes:
|
||||
if node.nodeType == node.TEXT_NODE:
|
||||
rc.append(node.data)
|
||||
return "".join(rc).strip()
|
||||
|
||||
def parse_manifest_json(content):
|
||||
"""Extract details from the contents of a WebExtensions manifest.json file."""
|
||||
manifest = json.loads(content)
|
||||
try:
|
||||
id = manifest["applications"]["gecko"]["id"]
|
||||
except KeyError:
|
||||
id = manifest["name"].replace(" ", "") + "@" + manifest["version"]
|
||||
return {
|
||||
"id": id,
|
||||
"version": manifest["version"],
|
||||
"name": manifest["version"],
|
||||
"unpack": False,
|
||||
}
|
||||
|
||||
if not os.path.exists(addon_path):
|
||||
raise OSError(f"Add-on path does not exist: {addon_path}")
|
||||
|
||||
try:
|
||||
if zipfile.is_zipfile(addon_path):
|
||||
with zipfile.ZipFile(addon_path, "r") as compressed_file:
|
||||
if "manifest.json" in compressed_file.namelist():
|
||||
return parse_manifest_json(compressed_file.read("manifest.json"))
|
||||
|
||||
manifest = compressed_file.read("install.rdf")
|
||||
elif os.path.isdir(addon_path):
|
||||
manifest_json_filename = os.path.join(addon_path, "manifest.json")
|
||||
if os.path.exists(manifest_json_filename):
|
||||
with open(manifest_json_filename, encoding="utf-8") as f:
|
||||
return parse_manifest_json(f.read())
|
||||
|
||||
with open(os.path.join(addon_path, "install.rdf"), encoding="utf-8") as f:
|
||||
manifest = f.read()
|
||||
else:
|
||||
raise OSError(f"Add-on path is neither an XPI nor a directory: {addon_path}")
|
||||
except (OSError, KeyError) as e:
|
||||
raise AddonFormatError(str(e), sys.exc_info()[2])
|
||||
|
||||
try:
|
||||
doc = minidom.parseString(manifest)
|
||||
|
||||
# Get the namespaces abbreviations
|
||||
em = get_namespace_id(doc, "http://www.mozilla.org/2004/em-rdf#")
|
||||
rdf = get_namespace_id(doc, "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
|
||||
|
||||
description = doc.getElementsByTagName(rdf + "Description").item(0)
|
||||
if not description:
|
||||
description = doc.getElementsByTagName("Description").item(0)
|
||||
for node in description.childNodes:
|
||||
# Remove the namespace prefix from the tag for comparison
|
||||
entry = node.nodeName.replace(em, "")
|
||||
if entry in details:
|
||||
details.update({entry: get_text(node)})
|
||||
if not details.get("id"):
|
||||
for i in range(description.attributes.length):
|
||||
attribute = description.attributes.item(i)
|
||||
if attribute.name == em + "id":
|
||||
details.update({"id": attribute.value})
|
||||
except Exception as e:
|
||||
raise AddonFormatError(str(e), sys.exc_info()[2])
|
||||
|
||||
# turn unpack into a true/false value
|
||||
if isinstance(details["unpack"], str):
|
||||
details["unpack"] = details["unpack"].lower() == "true"
|
||||
|
||||
# If no ID is set, the add-on is invalid
|
||||
if not details.get("id"):
|
||||
raise AddonFormatError("Add-on id could not be found.")
|
||||
|
||||
return details
|
||||
@@ -0,0 +1,115 @@
|
||||
# Licensed to the Software Freedom Conservancy (SFC) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The SFC licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from typing import Any
|
||||
|
||||
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
||||
from selenium.webdriver.common.options import ArgOptions
|
||||
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
|
||||
|
||||
|
||||
class Log:
|
||||
def __init__(self) -> None:
|
||||
self.level = None
|
||||
|
||||
def to_capabilities(self) -> dict:
|
||||
if self.level:
|
||||
return {"log": {"level": self.level}}
|
||||
return {}
|
||||
|
||||
|
||||
class Options(ArgOptions):
|
||||
KEY = "moz:firefoxOptions"
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self._binary_location = ""
|
||||
self._preferences: dict = {}
|
||||
# https://fxdx.dev/deprecating-cdp-support-in-firefox-embracing-the-future-with-webdriver-bidi/.
|
||||
# Enable BiDi only
|
||||
self._preferences["remote.active-protocols"] = 1
|
||||
self._profile: FirefoxProfile | None = None
|
||||
self.log = Log()
|
||||
|
||||
@property
|
||||
def binary_location(self) -> str:
|
||||
"""Returns the location of the binary."""
|
||||
return self._binary_location
|
||||
|
||||
@binary_location.setter
|
||||
def binary_location(self, value: str) -> None:
|
||||
"""Sets the location of the browser binary by string."""
|
||||
if not isinstance(value, str):
|
||||
raise TypeError(self.BINARY_LOCATION_ERROR)
|
||||
self._binary_location = value
|
||||
|
||||
@property
|
||||
def preferences(self) -> dict:
|
||||
"""Returns a dict of preferences."""
|
||||
return self._preferences
|
||||
|
||||
def set_preference(self, name: str, value: str | int | bool):
|
||||
"""Sets a preference."""
|
||||
self._preferences[name] = value
|
||||
|
||||
@property
|
||||
def profile(self) -> FirefoxProfile | None:
|
||||
"""Returns the Firefox profile to use."""
|
||||
return self._profile
|
||||
|
||||
@profile.setter
|
||||
def profile(self, new_profile: str | FirefoxProfile) -> None:
|
||||
"""Set the location of the browser profile to use (string or FirefoxProfile object)."""
|
||||
if not isinstance(new_profile, FirefoxProfile):
|
||||
new_profile = FirefoxProfile(new_profile)
|
||||
self._profile = new_profile
|
||||
|
||||
def enable_mobile(
|
||||
self, android_package: str | None = "org.mozilla.firefox", android_activity=None, device_serial=None
|
||||
):
|
||||
super().enable_mobile(android_package, android_activity, device_serial)
|
||||
|
||||
def to_capabilities(self) -> dict:
|
||||
"""Marshals the Firefox options to a `moz:firefoxOptions` object."""
|
||||
# This intentionally looks at the internal properties
|
||||
# so if a binary or profile has _not_ been set,
|
||||
# it will defer to geckodriver to find the system Firefox
|
||||
# and generate a fresh profile.
|
||||
caps = self._caps
|
||||
opts: dict[str, Any] = {}
|
||||
|
||||
if self._binary_location:
|
||||
opts["binary"] = self._binary_location
|
||||
if self._preferences:
|
||||
opts["prefs"] = self._preferences
|
||||
if self._profile:
|
||||
opts["profile"] = self._profile.encoded
|
||||
if self._arguments:
|
||||
opts["args"] = self._arguments
|
||||
if self.mobile_options:
|
||||
opts.update(self.mobile_options)
|
||||
|
||||
opts.update(self.log.to_capabilities())
|
||||
|
||||
if opts:
|
||||
caps[Options.KEY] = opts
|
||||
|
||||
return caps
|
||||
|
||||
@property
|
||||
def default_capabilities(self) -> dict:
|
||||
return DesiredCapabilities.FIREFOX.copy()
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
# Licensed to the Software Freedom Conservancy (SFC) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The SFC licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
|
||||
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
||||
from selenium.webdriver.remote.client_config import ClientConfig
|
||||
from selenium.webdriver.remote.remote_connection import RemoteConnection
|
||||
|
||||
|
||||
class FirefoxRemoteConnection(RemoteConnection):
|
||||
browser_name = DesiredCapabilities.FIREFOX["browserName"] # type: ignore
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
remote_server_addr: str,
|
||||
keep_alive: bool = True,
|
||||
ignore_proxy: bool = False,
|
||||
client_config: ClientConfig | None = None,
|
||||
) -> None:
|
||||
client_config = client_config or ClientConfig(
|
||||
remote_server_addr=remote_server_addr, keep_alive=keep_alive, timeout=120
|
||||
)
|
||||
super().__init__(
|
||||
ignore_proxy=ignore_proxy,
|
||||
client_config=client_config,
|
||||
)
|
||||
|
||||
self._commands["GET_CONTEXT"] = ("GET", "/session/$sessionId/moz/context")
|
||||
self._commands["SET_CONTEXT"] = ("POST", "/session/$sessionId/moz/context")
|
||||
self._commands["INSTALL_ADDON"] = ("POST", "/session/$sessionId/moz/addon/install")
|
||||
self._commands["UNINSTALL_ADDON"] = ("POST", "/session/$sessionId/moz/addon/uninstall")
|
||||
self._commands["FULL_PAGE_SCREENSHOT"] = ("GET", "/session/$sessionId/moz/screenshot/full")
|
||||
@@ -0,0 +1,96 @@
|
||||
# Licensed to the Software Freedom Conservancy (SFC) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The SFC licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from collections.abc import Mapping, Sequence
|
||||
from typing import IO, Any
|
||||
|
||||
from selenium.webdriver.common import service, utils
|
||||
|
||||
|
||||
class Service(service.Service):
|
||||
"""Service class responsible for starting and stopping of `geckodriver`.
|
||||
|
||||
Args:
|
||||
executable_path: (Optional) Install path of the executable.
|
||||
port: (Optional) Port for the service to run on, defaults to 0 where the operating system will decide.
|
||||
service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.
|
||||
log_output: (Optional) int representation of STDOUT/DEVNULL, any IO instance or String path to file.
|
||||
env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
|
||||
driver_path_env_key: (Optional) Environment variable to use to get the path to the driver executable.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
executable_path: str | None = None,
|
||||
port: int = 0,
|
||||
service_args: Sequence[str] | None = None,
|
||||
log_output: int | str | IO[Any] | None = None,
|
||||
env: Mapping[str, str] | None = None,
|
||||
driver_path_env_key: str | None = None,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
self._service_args = list(service_args or [])
|
||||
driver_path_env_key = driver_path_env_key or "SE_GECKODRIVER"
|
||||
|
||||
if os.environ.get("SE_DEBUG"):
|
||||
has_log_arg = "--log" in self._service_args or any(arg.startswith("--log=") for arg in self._service_args)
|
||||
has_output_conflict = log_output is not None
|
||||
if has_log_arg or has_output_conflict:
|
||||
logging.getLogger(__name__).warning(
|
||||
"Environment Variable `SE_DEBUG` is set; "
|
||||
"forcing GeckoDriver log level to DEBUG and overriding configured log level/output."
|
||||
)
|
||||
if has_log_arg:
|
||||
if "--log" in self._service_args:
|
||||
idx = self._service_args.index("--log")
|
||||
del self._service_args[idx : idx + 2]
|
||||
else:
|
||||
self._service_args = [arg for arg in self._service_args if not arg.startswith("--log=")]
|
||||
self._service_args.append("--log")
|
||||
self._service_args.append("debug")
|
||||
log_output = sys.stderr
|
||||
|
||||
super().__init__(
|
||||
executable_path=executable_path,
|
||||
port=port,
|
||||
log_output=log_output,
|
||||
env=env,
|
||||
driver_path_env_key=driver_path_env_key,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
# Set a port for CDP
|
||||
if "--connect-existing" not in self._service_args:
|
||||
self._service_args.append("--websocket-port")
|
||||
self._service_args.append(f"{utils.free_port()}")
|
||||
|
||||
def command_line_args(self) -> list[str]:
|
||||
return ["--port", f"{self.port}"] + self._service_args
|
||||
|
||||
@property
|
||||
def service_args(self) -> Sequence[str]:
|
||||
"""Returns the sequence of service arguments."""
|
||||
return self._service_args
|
||||
|
||||
@service_args.setter
|
||||
def service_args(self, value: Sequence[str]):
|
||||
if isinstance(value, str) or not isinstance(value, Sequence):
|
||||
raise TypeError("service_args must be a sequence")
|
||||
self._service_args = list(value)
|
||||
@@ -0,0 +1,214 @@
|
||||
# Licensed to the Software Freedom Conservancy (SFC) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The SFC licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import base64
|
||||
import os
|
||||
import warnings
|
||||
import zipfile
|
||||
from contextlib import contextmanager
|
||||
from io import BytesIO
|
||||
|
||||
from selenium.webdriver.common.driver_finder import DriverFinder
|
||||
from selenium.webdriver.common.webdriver import LocalWebDriver
|
||||
from selenium.webdriver.firefox.options import Options
|
||||
from selenium.webdriver.firefox.remote_connection import FirefoxRemoteConnection
|
||||
from selenium.webdriver.firefox.service import Service
|
||||
|
||||
|
||||
class WebDriver(LocalWebDriver):
|
||||
"""Controls the GeckoDriver and allows you to drive the browser."""
|
||||
|
||||
CONTEXT_CHROME = "chrome"
|
||||
CONTEXT_CONTENT = "content"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
options: Options | None = None,
|
||||
service: Service | None = None,
|
||||
keep_alive: bool = True,
|
||||
) -> None:
|
||||
"""Create a new instance of the Firefox driver, start the service, and create new instance.
|
||||
|
||||
Args:
|
||||
options: Instance of Options.
|
||||
service: Service object for handling the browser driver if you need to pass extra details.
|
||||
keep_alive: Whether to configure FirefoxRemoteConnection to use HTTP keep-alive.
|
||||
"""
|
||||
self.service = service if service else Service()
|
||||
self.options = options if options else Options()
|
||||
|
||||
finder = DriverFinder(self.service, self.options)
|
||||
if finder.get_browser_path():
|
||||
self.options.binary_location = finder.get_browser_path()
|
||||
self.options.browser_version = None
|
||||
|
||||
self.service.path = self.service.env_path() or finder.get_driver_path()
|
||||
self.service.start()
|
||||
|
||||
executor = FirefoxRemoteConnection(
|
||||
remote_server_addr=self.service.service_url,
|
||||
keep_alive=keep_alive,
|
||||
ignore_proxy=self.options._ignore_local_proxy,
|
||||
)
|
||||
|
||||
try:
|
||||
super().__init__(command_executor=executor, options=self.options)
|
||||
except Exception:
|
||||
self.quit()
|
||||
raise
|
||||
|
||||
def set_context(self, context) -> None:
|
||||
"""Sets the context that Selenium commands are running in.
|
||||
|
||||
Args:
|
||||
context: Context to set, should be one of CONTEXT_CHROME or CONTEXT_CONTENT.
|
||||
"""
|
||||
self.execute("SET_CONTEXT", {"context": context})
|
||||
|
||||
@contextmanager
|
||||
def context(self, context):
|
||||
"""Set the context that Selenium commands are running in using a `with` statement.
|
||||
|
||||
The state of the context on the server is saved before entering the block,
|
||||
and restored upon exiting it.
|
||||
|
||||
Args:
|
||||
context: Context, may be one of the class properties
|
||||
`CONTEXT_CHROME` or `CONTEXT_CONTENT`.
|
||||
|
||||
Example:
|
||||
with selenium.context(selenium.CONTEXT_CHROME):
|
||||
# chrome scope
|
||||
... do stuff ...
|
||||
"""
|
||||
initial_context = self.execute("GET_CONTEXT").pop("value")
|
||||
self.set_context(context)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
self.set_context(initial_context)
|
||||
|
||||
def install_addon(self, path, temporary=False) -> str:
|
||||
"""Installs Firefox addon.
|
||||
|
||||
Returns identifier of installed addon. This identifier can later
|
||||
be used to uninstall addon.
|
||||
|
||||
Args:
|
||||
path: Absolute path to the addon that will be installed.
|
||||
temporary: Allows you to load browser extensions temporarily during a session.
|
||||
|
||||
Returns:
|
||||
Identifier of installed addon.
|
||||
|
||||
Example:
|
||||
driver.install_addon("/path/to/firebug.xpi")
|
||||
"""
|
||||
if os.path.isdir(path):
|
||||
fp = BytesIO()
|
||||
# filter all trailing slash found in path
|
||||
path = os.path.normpath(path)
|
||||
# account for trailing slash that will be added by os.walk()
|
||||
path_root = len(path) + 1
|
||||
with zipfile.ZipFile(fp, "w", zipfile.ZIP_DEFLATED, strict_timestamps=False) as zipped:
|
||||
for base, _, files in os.walk(path):
|
||||
for fyle in files:
|
||||
filename = os.path.join(base, fyle)
|
||||
zipped.write(filename, filename[path_root:])
|
||||
addon = base64.b64encode(fp.getvalue()).decode("UTF-8")
|
||||
else:
|
||||
with open(path, "rb") as file:
|
||||
addon = base64.b64encode(file.read()).decode("UTF-8")
|
||||
|
||||
payload = {"addon": addon, "temporary": temporary}
|
||||
return self.execute("INSTALL_ADDON", payload)["value"]
|
||||
|
||||
def uninstall_addon(self, identifier) -> None:
|
||||
"""Uninstalls Firefox addon using its identifier.
|
||||
|
||||
Args:
|
||||
identifier: The addon identifier to uninstall.
|
||||
|
||||
Example:
|
||||
driver.uninstall_addon("addon@foo.com")
|
||||
"""
|
||||
self.execute("UNINSTALL_ADDON", {"id": identifier})
|
||||
|
||||
def get_full_page_screenshot_as_file(self, filename) -> bool:
|
||||
"""Save a full document screenshot of the current window to a PNG image file.
|
||||
|
||||
Args:
|
||||
filename: The full path you wish to save your screenshot to. This
|
||||
should end with a `.png` extension.
|
||||
|
||||
Returns:
|
||||
False if there is any IOError, else returns True. Use full paths in your filename.
|
||||
|
||||
Example:
|
||||
driver.get_full_page_screenshot_as_file("/Screenshots/foo.png")
|
||||
"""
|
||||
if not filename.lower().endswith(".png"):
|
||||
warnings.warn(
|
||||
"name used for saved screenshot does not match file type. It should end with a `.png` extension",
|
||||
UserWarning,
|
||||
)
|
||||
png = self.get_full_page_screenshot_as_png()
|
||||
try:
|
||||
with open(filename, "wb") as f:
|
||||
f.write(png)
|
||||
except OSError:
|
||||
return False
|
||||
finally:
|
||||
del png
|
||||
return True
|
||||
|
||||
def save_full_page_screenshot(self, filename) -> bool:
|
||||
"""Save a full document screenshot of the current window to a PNG image file.
|
||||
|
||||
Args:
|
||||
filename: The full path you wish to save your screenshot to. This
|
||||
should end with a `.png` extension.
|
||||
|
||||
Returns:
|
||||
False if there is any IOError, else returns True. Use full paths in your filename.
|
||||
|
||||
Example:
|
||||
driver.save_full_page_screenshot("/Screenshots/foo.png")
|
||||
"""
|
||||
return self.get_full_page_screenshot_as_file(filename)
|
||||
|
||||
def get_full_page_screenshot_as_png(self) -> bytes:
|
||||
"""Get the full document screenshot of the current window as binary data.
|
||||
|
||||
Returns:
|
||||
Binary data of the screenshot.
|
||||
|
||||
Example:
|
||||
driver.get_full_page_screenshot_as_png()
|
||||
"""
|
||||
return base64.b64decode(self.get_full_page_screenshot_as_base64().encode("ascii"))
|
||||
|
||||
def get_full_page_screenshot_as_base64(self) -> str:
|
||||
"""Get the full document screenshot of the current window as a base64-encoded string.
|
||||
|
||||
Returns:
|
||||
Base64 encoded string of the screenshot.
|
||||
|
||||
Example:
|
||||
driver.get_full_page_screenshot_as_base64()
|
||||
"""
|
||||
return self.execute("FULL_PAGE_SCREENSHOT")["value"]
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
{
|
||||
"frozen": {
|
||||
"app.update.auto": false,
|
||||
"app.update.enabled": false,
|
||||
"browser.displayedE10SNotice": 4,
|
||||
"browser.download.manager.showWhenStarting": false,
|
||||
"browser.EULA.override": true,
|
||||
"browser.EULA.3.accepted": true,
|
||||
"browser.link.open_external": 2,
|
||||
"browser.link.open_newwindow": 2,
|
||||
"browser.offline": false,
|
||||
"browser.reader.detectedFirstArticle": true,
|
||||
"browser.safebrowsing.enabled": false,
|
||||
"browser.safebrowsing.malware.enabled": false,
|
||||
"browser.search.update": false,
|
||||
"browser.selfsupport.url" : "",
|
||||
"browser.sessionstore.resume_from_crash": false,
|
||||
"browser.shell.checkDefaultBrowser": false,
|
||||
"browser.tabs.warnOnClose": false,
|
||||
"browser.tabs.warnOnOpen": false,
|
||||
"datareporting.healthreport.service.enabled": false,
|
||||
"datareporting.healthreport.uploadEnabled": false,
|
||||
"datareporting.healthreport.service.firstRun": false,
|
||||
"datareporting.healthreport.logging.consoleEnabled": false,
|
||||
"datareporting.policy.dataSubmissionEnabled": false,
|
||||
"datareporting.policy.dataSubmissionPolicyAccepted": false,
|
||||
"devtools.errorconsole.enabled": true,
|
||||
"dom.disable_open_during_load": false,
|
||||
"extensions.autoDisableScopes": 10,
|
||||
"extensions.blocklist.enabled": false,
|
||||
"extensions.checkCompatibility.nightly": false,
|
||||
"extensions.update.enabled": false,
|
||||
"extensions.update.notifyUser": false,
|
||||
"javascript.enabled": true,
|
||||
"network.manage-offline-status": false,
|
||||
"network.http.phishy-userpass-length": 255,
|
||||
"offline-apps.allow_by_default": true,
|
||||
"prompts.tab_modal.enabled": false,
|
||||
"security.fileuri.origin_policy": 3,
|
||||
"security.fileuri.strict_origin_policy": false,
|
||||
"signon.rememberSignons": false,
|
||||
"toolkit.networkmanager.disable": true,
|
||||
"toolkit.telemetry.prompted": 2,
|
||||
"toolkit.telemetry.enabled": false,
|
||||
"toolkit.telemetry.rejected": true,
|
||||
"xpinstall.signatures.required": false,
|
||||
"xpinstall.whitelist.required": false
|
||||
},
|
||||
"mutable": {
|
||||
"browser.dom.window.dump.enabled": true,
|
||||
"browser.laterrun.enabled": false,
|
||||
"browser.newtab.url": "about:blank",
|
||||
"browser.newtabpage.enabled": false,
|
||||
"browser.startup.page": 0,
|
||||
"browser.startup.homepage": "about:blank",
|
||||
"browser.startup.homepage_override.mstone": "ignore",
|
||||
"browser.usedOnWindows10.introURL": "about:blank",
|
||||
"dom.max_chrome_script_run_time": 30,
|
||||
"dom.max_script_run_time": 30,
|
||||
"dom.report_all_js_exceptions": true,
|
||||
"javascript.options.showInConsole": true,
|
||||
"network.captive-portal-service.enabled": false,
|
||||
"security.csp.enable": false,
|
||||
"startup.homepage_welcome_url": "about:blank",
|
||||
"startup.homepage_welcome_url.additional": "about:blank",
|
||||
"webdriver_accept_untrusted_certs": true,
|
||||
"webdriver_assume_untrusted_issuer": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user