Path: blob/trunk/py/selenium/webdriver/firefox/webdriver.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.16import base6417import os18import warnings19import zipfile20from contextlib import contextmanager21from io import BytesIO22from typing import Optional2324from selenium.webdriver.common.driver_finder import DriverFinder25from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver2627from .options import Options28from .remote_connection import FirefoxRemoteConnection29from .service import Service303132class WebDriver(RemoteWebDriver):33"""Controls the GeckoDriver and allows you to drive the browser."""3435CONTEXT_CHROME = "chrome"36CONTEXT_CONTENT = "content"3738def __init__(39self,40options: Optional[Options] = None,41service: Optional[Service] = None,42keep_alive: bool = True,43) -> None:44"""Creates a new instance of the Firefox driver. Starts the service and45then creates new instance of Firefox driver.4647:Args:48- options - Instance of ``options.Options``.49- service - (Optional) service instance for managing the starting and stopping of the driver.50- keep_alive - Whether to configure remote_connection.RemoteConnection to use HTTP keep-alive.51"""5253self.service = service if service else Service()54options = options if options else Options()5556finder = DriverFinder(self.service, options)57if finder.get_browser_path():58options.binary_location = finder.get_browser_path()59options.browser_version = None6061self.service.path = self.service.env_path() or finder.get_driver_path()62self.service.start()6364executor = FirefoxRemoteConnection(65remote_server_addr=self.service.service_url,66keep_alive=keep_alive,67ignore_proxy=options._ignore_local_proxy,68)6970try:71super().__init__(command_executor=executor, options=options)72except Exception:73self.quit()74raise7576self._is_remote = False7778def quit(self) -> None:79"""Closes the browser and shuts down the GeckoDriver executable."""80try:81super().quit()82except Exception:83# We don't care about the message because something probably has gone wrong84pass85finally:86self.service.stop()8788def set_context(self, context) -> None:89self.execute("SET_CONTEXT", {"context": context})9091@contextmanager92def context(self, context):93"""Sets the context that Selenium commands are running in using a94`with` statement. The state of the context on the server is saved95before entering the block, and restored upon exiting it.9697:param context: Context, may be one of the class properties98`CONTEXT_CHROME` or `CONTEXT_CONTENT`.99100Usage example::101102with selenium.context(selenium.CONTEXT_CHROME):103# chrome scope104... do stuff ...105"""106initial_context = self.execute("GET_CONTEXT").pop("value")107self.set_context(context)108try:109yield110finally:111self.set_context(initial_context)112113def install_addon(self, path, temporary=False) -> str:114"""Installs Firefox addon.115116Returns identifier of installed addon. This identifier can later117be used to uninstall addon.118119:param temporary: allows you to load browser extensions temporarily during a session120:param path: Absolute path to the addon that will be installed.121122:Usage:123::124125driver.install_addon("/path/to/firebug.xpi")126"""127128if os.path.isdir(path):129fp = BytesIO()130# filter all trailing slash found in path131path = os.path.normpath(path)132# account for trailing slash that will be added by os.walk()133path_root = len(path) + 1134with zipfile.ZipFile(fp, "w", zipfile.ZIP_DEFLATED, strict_timestamps=False) as zipped:135for base, _, files in os.walk(path):136for fyle in files:137filename = os.path.join(base, fyle)138zipped.write(filename, filename[path_root:])139addon = base64.b64encode(fp.getvalue()).decode("UTF-8")140else:141with open(path, "rb") as file:142addon = base64.b64encode(file.read()).decode("UTF-8")143144payload = {"addon": addon, "temporary": temporary}145return self.execute("INSTALL_ADDON", payload)["value"]146147def uninstall_addon(self, identifier) -> None:148"""Uninstalls Firefox addon using its identifier.149150:Usage:151::152153driver.uninstall_addon("[email protected]")154"""155self.execute("UNINSTALL_ADDON", {"id": identifier})156157def get_full_page_screenshot_as_file(self, filename) -> bool:158"""Saves a full document screenshot of the current window to a PNG159image file. Returns False if there is any IOError, else returns True.160Use full paths in your filename.161162:Args:163- filename: The full path you wish to save your screenshot to. This164should end with a `.png` extension.165166:Usage:167::168169driver.get_full_page_screenshot_as_file("/Screenshots/foo.png")170"""171if not filename.lower().endswith(".png"):172warnings.warn(173"name used for saved screenshot does not match file type. It should end with a `.png` extension",174UserWarning,175)176png = self.get_full_page_screenshot_as_png()177try:178with open(filename, "wb") as f:179f.write(png)180except OSError:181return False182finally:183del png184return True185186def save_full_page_screenshot(self, filename) -> bool:187"""Saves a full document screenshot of the current window to a PNG188image file. Returns False if there is any IOError, else returns True.189Use full paths in your filename.190191:Args:192- filename: The full path you wish to save your screenshot to. This193should end with a `.png` extension.194195:Usage:196::197198driver.save_full_page_screenshot("/Screenshots/foo.png")199"""200return self.get_full_page_screenshot_as_file(filename)201202def get_full_page_screenshot_as_png(self) -> bytes:203"""Gets the full document screenshot of the current window as a binary204data.205206:Usage:207::208209driver.get_full_page_screenshot_as_png()210"""211return base64.b64decode(self.get_full_page_screenshot_as_base64().encode("ascii"))212213def get_full_page_screenshot_as_base64(self) -> str:214"""Gets the full document screenshot of the current window as a base64215encoded string which is useful in embedded images in HTML.216217:Usage:218::219220driver.get_full_page_screenshot_as_base64()221"""222return self.execute("FULL_PAGE_SCREENSHOT")["value"]223224def download_file(self, *args, **kwargs):225raise NotImplementedError226227def get_downloadable_files(self, *args, **kwargs):228raise NotImplementedError229230231