From fc5485c6057da52d80e5aa9ddcba1fb71e15b2ab Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Thu, 13 Aug 2020 17:19:39 +0200 Subject: [PATCH] enh: cleanup of path and pathlib usages (#166) --- playwright/async_api.py | 84 +++++++++++++++++++++-------------- playwright/browser_context.py | 5 ++- playwright/browser_type.py | 18 +++++--- playwright/download.py | 2 +- playwright/element_handle.py | 13 ++++-- playwright/frame.py | 8 +++- playwright/page.py | 17 ++++--- playwright/selectors.py | 5 ++- playwright/sync_api.py | 84 +++++++++++++++++++++-------------- 9 files changed, 149 insertions(+), 87 deletions(-) diff --git a/playwright/async_api.py b/playwright/async_api.py index 323de3c25..4bb1831e7 100644 --- a/playwright/async_api.py +++ b/playwright/async_api.py @@ -1295,7 +1295,7 @@ async def screenshot( self, timeout: int = None, type: Literal["png", "jpeg"] = None, - path: str = None, + path: typing.Union[str, pathlib.Path] = None, quality: int = None, omitBackground: bool = None, ) -> bytes: @@ -1309,7 +1309,7 @@ async def screenshot( Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the browserContext.setDefaultTimeout(timeout) or page.setDefaultTimeout(timeout) methods. type : Optional[Literal['png', 'jpeg']] Specify screenshot type, defaults to `png`. - path : Optional[str] + path : Union[str, pathlib.Path, NoneType] The file path to save the image to. The screenshot type will be inferred from file extension. If `path` is a relative path, then it is resolved relative to current working directory. If no path is provided, the image won't be saved to the disk. quality : Optional[int] The quality of the image, between 0-100. Not applicable to `png` images. @@ -2039,7 +2039,11 @@ def isDetached(self) -> bool: return mapping.from_maybe_impl(self._impl_obj.isDetached()) async def addScriptTag( - self, url: str = None, path: str = None, content: str = None, type: str = None + self, + url: str = None, + path: typing.Union[str, pathlib.Path] = None, + content: str = None, + type: str = None, ) -> "ElementHandle": """Frame.addScriptTag @@ -2049,7 +2053,7 @@ async def addScriptTag( ---------- url : Optional[str] URL of a script to be added. - path : Optional[str] + path : Union[str, pathlib.Path, NoneType] Path to the JavaScript file to be injected into frame. If `path` is a relative path, then it is resolved relative to current working directory. content : Optional[str] Raw JavaScript content to be injected into frame. @@ -2068,7 +2072,10 @@ async def addScriptTag( ) async def addStyleTag( - self, url: str = None, path: str = None, content: str = None + self, + url: str = None, + path: typing.Union[str, pathlib.Path] = None, + content: str = None, ) -> "ElementHandle": """Frame.addStyleTag @@ -2078,7 +2085,7 @@ async def addStyleTag( ---------- url : Optional[str] URL of the `` tag. - path : Optional[str] + path : Union[str, pathlib.Path, NoneType] Path to the CSS file to be injected into frame. If `path` is a relative path, then it is resolved relative to current working directory. content : Optional[str] Raw CSS content to be injected into frame. @@ -2748,7 +2755,7 @@ async def register( self, name: str, source: str = None, - path: str = None, + path: typing.Union[str, pathlib.Path] = None, contentScript: bool = None, ) -> NoneType: """Selectors.register @@ -2939,14 +2946,14 @@ async def path(self) -> typing.Union[str, NoneType]: """ return mapping.from_maybe_impl(await self._impl_obj.path()) - async def saveAs(self, path: typing.Union[pathlib.Path, str]) -> NoneType: + async def saveAs(self, path: typing.Union[str, pathlib.Path]) -> NoneType: """Download.saveAs Saves the download to a user-specified path. Parameters ---------- - path : Union[pathlib.Path, str] + path : Union[str, pathlib.Path] Path where the download should be saved. """ return mapping.from_maybe_impl(await self._impl_obj.saveAs(path=path)) @@ -3397,7 +3404,11 @@ async def evalOnSelectorAll( ) async def addScriptTag( - self, url: str = None, path: str = None, content: str = None, type: str = None + self, + url: str = None, + path: typing.Union[str, pathlib.Path] = None, + content: str = None, + type: str = None, ) -> "ElementHandle": """Page.addScriptTag @@ -3408,7 +3419,7 @@ async def addScriptTag( ---------- url : Optional[str] URL of a script to be added. - path : Optional[str] + path : Union[str, pathlib.Path, NoneType] Path to the JavaScript file to be injected into frame. If `path` is a relative path, then it is resolved relative to current working directory. content : Optional[str] Raw JavaScript content to be injected into frame. @@ -3427,7 +3438,10 @@ async def addScriptTag( ) async def addStyleTag( - self, url: str = None, path: str = None, content: str = None + self, + url: str = None, + path: typing.Union[str, pathlib.Path] = None, + content: str = None, ) -> "ElementHandle": """Page.addStyleTag @@ -3438,7 +3452,7 @@ async def addStyleTag( ---------- url : Optional[str] URL of the `` tag. - path : Optional[str] + path : Union[str, pathlib.Path, NoneType] Path to the CSS file to be injected into frame. If `path` is a relative path, then it is resolved relative to current working directory. content : Optional[str] Raw CSS content to be injected into frame. @@ -3890,7 +3904,9 @@ async def bringToFront(self) -> NoneType: """ return mapping.from_maybe_impl(await self._impl_obj.bringToFront()) - async def addInitScript(self, source: str = None, path: str = None) -> NoneType: + async def addInitScript( + self, source: str = None, path: typing.Union[str, pathlib.Path] = None + ) -> NoneType: """Page.addInitScript Adds a script which would be evaluated in one of the following scenarios: @@ -3966,7 +3982,7 @@ async def screenshot( self, timeout: int = None, type: Literal["png", "jpeg"] = None, - path: str = None, + path: typing.Union[str, pathlib.Path] = None, quality: int = None, omitBackground: bool = None, fullPage: bool = None, @@ -3982,7 +3998,7 @@ async def screenshot( Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the browserContext.setDefaultTimeout(timeout) or page.setDefaultTimeout(timeout) methods. type : Optional[Literal['png', 'jpeg']] Specify screenshot type, defaults to `png`. - path : Optional[str] + path : Union[str, pathlib.Path, NoneType] The file path to save the image to. The screenshot type will be inferred from file extension. If `path` is a relative path, then it is resolved relative to current working directory. If no path is provided, the image won't be saved to the disk. quality : Optional[int] The quality of the image, between 0-100. Not applicable to `png` images. @@ -4618,7 +4634,7 @@ async def pdf( height: typing.Union[str, float] = None, preferCSSPageSize: bool = None, margin: PdfMargins = None, - path: str = None, + path: typing.Union[str, pathlib.Path] = None, ) -> bytes: """Page.pdf @@ -4692,7 +4708,7 @@ async def pdf( Give any CSS `@page` size declared in the page priority over what is declared in `width` and `height` or `format` options. Defaults to `false`, which will scale the content to fit the paper size. margin : Optional[{"top": Union[str, int, NoneType], "right": Union[str, int, NoneType], "bottom": Union[str, int, NoneType], "left": Union[str, int, NoneType]}] Paper margins, defaults to none. - path : Optional[str] + path : Union[str, pathlib.Path, NoneType] The file path to save the PDF to. If `path` is a relative path, then it is resolved relative to current working directory. If no path is provided, the PDF won't be saved to the disk. Returns @@ -5016,7 +5032,9 @@ async def setOffline(self, offline: bool) -> NoneType: """ return mapping.from_maybe_impl(await self._impl_obj.setOffline(offline=offline)) - async def addInitScript(self, source: str = None, path: str = None) -> NoneType: + async def addInitScript( + self, source: str = None, path: typing.Union[str, pathlib.Path] = None + ) -> NoneType: """BrowserContext.addInitScript Adds a script which would be evaluated in one of the following scenarios: @@ -5568,7 +5586,7 @@ def executablePath(self) -> str: async def launch( self, - executablePath: str = None, + executablePath: typing.Union[str, pathlib.Path] = None, args: typing.List[str] = None, ignoreDefaultArgs: typing.Union[bool, typing.List[str]] = None, handleSIGINT: bool = None, @@ -5579,7 +5597,7 @@ async def launch( headless: bool = None, devtools: bool = None, proxy: ProxyServer = None, - downloadsPath: str = None, + downloadsPath: typing.Union[str, pathlib.Path] = None, slowMo: int = None, chromiumSandbox: bool = None, ) -> "Browser": @@ -5594,7 +5612,7 @@ async def launch( Parameters ---------- - executablePath : Optional[str] + executablePath : Union[str, pathlib.Path, NoneType] Path to a browser executable to run instead of the bundled one. If `executablePath` is a relative path, then it is resolved relative to current working directory. Note that Playwright only works with the bundled Chromium, Firefox or WebKit, use at your own risk. args : Optional[List[str]] Additional arguments to pass to the browser instance. The list of Chromium flags can be found here. @@ -5616,7 +5634,7 @@ async def launch( **Chromium-only** Whether to auto-open a Developer Tools panel for each tab. If this option is `true`, the `headless` option will be set `false`. proxy : Optional[{"server": str, "bypass": Optional[str], "username": Optional[str], "password": Optional[str]}] Network proxy settings. - downloadsPath : Optional[str] + downloadsPath : Union[str, pathlib.Path, NoneType] If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed. slowMo : Optional[int] Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on. @@ -5649,7 +5667,7 @@ async def launch( async def launchServer( self, - executablePath: str = None, + executablePath: typing.Union[str, pathlib.Path] = None, args: typing.List[str] = None, ignoreDefaultArgs: typing.Union[bool, typing.List[str]] = None, handleSIGINT: bool = None, @@ -5660,7 +5678,7 @@ async def launchServer( headless: bool = None, devtools: bool = None, proxy: ProxyServer = None, - downloadsPath: str = None, + downloadsPath: typing.Union[str, pathlib.Path] = None, port: int = None, chromiumSandbox: bool = None, ) -> "BrowserServer": @@ -5670,7 +5688,7 @@ async def launchServer( Parameters ---------- - executablePath : Optional[str] + executablePath : Union[str, pathlib.Path, NoneType] Path to a browser executable to run instead of the bundled one. If `executablePath` is a relative path, then it is resolved relative to current working directory. **BEWARE**: Playwright is only guaranteed to work with the bundled Chromium, Firefox or WebKit, use at your own risk. args : Optional[List[str]] Additional arguments to pass to the browser instance. The list of Chromium flags can be found here. @@ -5692,7 +5710,7 @@ async def launchServer( **Chromium-only** Whether to auto-open a Developer Tools panel for each tab. If this option is `true`, the `headless` option will be set `false`. proxy : Optional[{"server": str, "bypass": Optional[str], "username": Optional[str], "password": Optional[str]}] Network proxy settings. - downloadsPath : Optional[str] + downloadsPath : Union[str, pathlib.Path, NoneType] If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed. port : Optional[int] Port to use for the web socket. Defaults to 0 that picks any available port. @@ -5725,8 +5743,8 @@ async def launchServer( async def launchPersistentContext( self, - userDataDir: str, - executablePath: str = None, + userDataDir: typing.Union[str, pathlib.Path], + executablePath: typing.Union[str, pathlib.Path] = None, args: typing.List[str] = None, ignoreDefaultArgs: typing.Union[bool, typing.List[str]] = None, handleSIGINT: bool = None, @@ -5737,7 +5755,7 @@ async def launchPersistentContext( headless: bool = None, devtools: bool = None, proxy: ProxyServer = None, - downloadsPath: str = None, + downloadsPath: typing.Union[str, pathlib.Path] = None, slowMo: int = None, viewport: IntSize = None, ignoreHTTPSErrors: bool = None, @@ -5764,9 +5782,9 @@ async def launchPersistentContext( Parameters ---------- - userDataDir : str + userDataDir : Union[str, pathlib.Path] Path to a User Data Directory, which stores browser session data like cookies and local storage. More details for Chromium and Firefox. - executablePath : Optional[str] + executablePath : Union[str, pathlib.Path, NoneType] Path to a browser executable to run instead of the bundled one. If `executablePath` is a relative path, then it is resolved relative to current working directory. **BEWARE**: Playwright is only guaranteed to work with the bundled Chromium, Firefox or WebKit, use at your own risk. args : Optional[List[str]] Additional arguments to pass to the browser instance. The list of Chromium flags can be found here. @@ -5788,7 +5806,7 @@ async def launchPersistentContext( **Chromium-only** Whether to auto-open a Developer Tools panel for each tab. If this option is `true`, the `headless` option will be set `false`. proxy : Optional[{"server": str, "bypass": Optional[str], "username": Optional[str], "password": Optional[str]}] Network proxy settings. - downloadsPath : Optional[str] + downloadsPath : Union[str, pathlib.Path, NoneType] If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed. slowMo : Optional[int] Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on. Defaults to 0. diff --git a/playwright/browser_context.py b/playwright/browser_context.py index be2490441..d00b1bf6a 100644 --- a/playwright/browser_context.py +++ b/playwright/browser_context.py @@ -13,6 +13,7 @@ # limitations under the License. import asyncio +from pathlib import Path from types import SimpleNamespace from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union @@ -139,7 +140,9 @@ async def setExtraHTTPHeaders(self, headers: Dict[str, str]) -> None: async def setOffline(self, offline: bool) -> None: await self._channel.send("setOffline", dict(offline=offline)) - async def addInitScript(self, source: str = None, path: str = None) -> None: + async def addInitScript( + self, source: str = None, path: Union[str, Path] = None + ) -> None: if path: with open(path, "r") as file: source = file.read() diff --git a/playwright/browser_type.py b/playwright/browser_type.py index d2a53d8b5..cf7075c57 100644 --- a/playwright/browser_type.py +++ b/playwright/browser_type.py @@ -48,7 +48,7 @@ def executablePath(self) -> str: async def launch( self, - executablePath: str = None, + executablePath: Union[str, Path] = None, args: List[str] = None, ignoreDefaultArgs: Union[bool, List[str]] = None, handleSIGINT: bool = None, @@ -59,7 +59,7 @@ async def launch( headless: bool = None, devtools: bool = None, proxy: ProxyServer = None, - downloadsPath: str = None, + downloadsPath: Union[str, Path] = None, slowMo: int = None, chromiumSandbox: bool = None, ) -> Browser: @@ -74,7 +74,7 @@ async def launch( async def launchServer( self, - executablePath: str = None, + executablePath: Union[str, Path] = None, args: List[str] = None, ignoreDefaultArgs: Union[bool, List[str]] = None, handleSIGINT: bool = None, @@ -85,7 +85,7 @@ async def launchServer( headless: bool = None, devtools: bool = None, proxy: ProxyServer = None, - downloadsPath: str = None, + downloadsPath: Union[str, Path] = None, port: int = None, chromiumSandbox: bool = None, ) -> BrowserServer: @@ -100,8 +100,8 @@ async def launchServer( async def launchPersistentContext( self, - userDataDir: str, - executablePath: str = None, + userDataDir: Union[str, Path], + executablePath: Union[str, Path] = None, args: List[str] = None, ignoreDefaultArgs: Union[bool, List[str]] = None, handleSIGINT: bool = None, @@ -112,7 +112,7 @@ async def launchPersistentContext( headless: bool = None, devtools: bool = None, proxy: ProxyServer = None, - downloadsPath: str = None, + downloadsPath: Union[str, Path] = None, slowMo: int = None, viewport: IntSize = None, ignoreHTTPSErrors: bool = None, @@ -163,3 +163,7 @@ def normalize_launch_params(params: Dict) -> None: params["ignoreAllDefaultArgs"] = True del params["ignoreDefaultArgs"] params["env"] = {name: str(value) for [name, value] in params["env"].items()} + if "executablePath" in params: + params["executablePath"] = str(Path(params["executablePath"])) + if "downloadsPath" in params: + params["downloadsPath"] = str(Path(params["downloadsPath"])) diff --git a/playwright/download.py b/playwright/download.py index 1c183fd86..6da4de75e 100644 --- a/playwright/download.py +++ b/playwright/download.py @@ -41,6 +41,6 @@ async def failure(self) -> Optional[str]: async def path(self) -> Optional[str]: return await self._channel.send("path") - async def saveAs(self, path: Union[Path, str]) -> None: + async def saveAs(self, path: Union[str, Path]) -> None: path = str(Path(path)) return await self._channel.send("saveAs", dict(path=path)) diff --git a/playwright/element_handle.py b/playwright/element_handle.py index 3986752f6..f90b41644 100644 --- a/playwright/element_handle.py +++ b/playwright/element_handle.py @@ -175,12 +175,19 @@ async def screenshot( self, timeout: int = None, type: Literal["png", "jpeg"] = None, - path: str = None, + path: Union[str, Path] = None, quality: int = None, omitBackground: bool = None, ) -> bytes: - binary = await self._channel.send("screenshot", locals_to_params(locals())) - return base64.b64decode(binary) + params = locals_to_params(locals()) + if "path" in params: + del params["path"] + encoded_binary = await self._channel.send("screenshot", params) + decoded_binary = base64.b64decode(encoded_binary) + if path: + with open(path, "wb") as fd: + fd.write(decoded_binary) + return decoded_binary async def querySelector(self, selector: str) -> Optional["ElementHandle"]: return from_nullable_channel( diff --git a/playwright/frame.py b/playwright/frame.py index 3cf200717..b548f4acd 100644 --- a/playwright/frame.py +++ b/playwright/frame.py @@ -314,7 +314,11 @@ def isDetached(self) -> bool: return self._detached async def addScriptTag( - self, url: str = None, path: str = None, content: str = None, type: str = None, + self, + url: str = None, + path: Union[str, Path] = None, + content: str = None, + type: str = None, ) -> ElementHandle: params = locals_to_params(locals()) if path: @@ -324,7 +328,7 @@ async def addScriptTag( return from_channel(await self._channel.send("addScriptTag", params)) async def addStyleTag( - self, url: str = None, path: str = None, content: str = None + self, url: str = None, path: Union[str, Path] = None, content: str = None ) -> ElementHandle: params = locals_to_params(locals()) if path: diff --git a/playwright/page.py b/playwright/page.py index 45ae02bed..9a19baafd 100644 --- a/playwright/page.py +++ b/playwright/page.py @@ -15,6 +15,7 @@ import asyncio import base64 import sys +from pathlib import Path from types import SimpleNamespace from typing import TYPE_CHECKING, Any, Callable, Dict, List, Union, cast @@ -342,12 +343,16 @@ async def evalOnSelectorAll( ) async def addScriptTag( - self, url: str = None, path: str = None, content: str = None, type: str = None + self, + url: str = None, + path: Union[str, Path] = None, + content: str = None, + type: str = None, ) -> ElementHandle: return await self._main_frame.addScriptTag(**locals_to_params(locals())) async def addStyleTag( - self, url: str = None, path: str = None, content: str = None + self, url: str = None, path: Union[str, Path] = None, content: str = None ) -> ElementHandle: return await self._main_frame.addStyleTag(**locals_to_params(locals())) @@ -500,7 +505,9 @@ def viewportSize(self) -> Optional[Viewport]: async def bringToFront(self) -> None: await self._channel.send("bringToFront") - async def addInitScript(self, source: str = None, path: str = None) -> None: + async def addInitScript( + self, source: str = None, path: Union[str, Path] = None + ) -> None: if path: with open(path, "r") as file: source = file.read() @@ -533,7 +540,7 @@ async def screenshot( self, timeout: int = None, type: Literal["png", "jpeg"] = None, - path: str = None, + path: Union[str, Path] = None, quality: int = None, omitBackground: bool = None, fullPage: bool = None, @@ -726,7 +733,7 @@ async def pdf( height: Union[str, float] = None, preferCSSPageSize: bool = None, margin: PdfMargins = None, - path: str = None, + path: Union[str, Path] = None, ) -> bytes: params = locals_to_params(locals()) if "path" in params: diff --git a/playwright/selectors.py b/playwright/selectors.py index e3f3d8aa9..0154ff964 100644 --- a/playwright/selectors.py +++ b/playwright/selectors.py @@ -12,7 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Dict, Optional +from pathlib import Path +from typing import Dict, Optional, Union from playwright.connection import ChannelOwner from playwright.element_handle import ElementHandle @@ -29,7 +30,7 @@ async def register( self, name: str, source: str = None, - path: str = None, + path: Union[str, Path] = None, contentScript: bool = None, ) -> None: if not source and not path: diff --git a/playwright/sync_api.py b/playwright/sync_api.py index ad9a2363a..f6473cefe 100644 --- a/playwright/sync_api.py +++ b/playwright/sync_api.py @@ -1343,7 +1343,7 @@ def screenshot( self, timeout: int = None, type: Literal["png", "jpeg"] = None, - path: str = None, + path: typing.Union[str, pathlib.Path] = None, quality: int = None, omitBackground: bool = None, ) -> bytes: @@ -1357,7 +1357,7 @@ def screenshot( Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the browserContext.setDefaultTimeout(timeout) or page.setDefaultTimeout(timeout) methods. type : Optional[Literal['png', 'jpeg']] Specify screenshot type, defaults to `png`. - path : Optional[str] + path : Union[str, pathlib.Path, NoneType] The file path to save the image to. The screenshot type will be inferred from file extension. If `path` is a relative path, then it is resolved relative to current working directory. If no path is provided, the image won't be saved to the disk. quality : Optional[int] The quality of the image, between 0-100. Not applicable to `png` images. @@ -2115,7 +2115,11 @@ def isDetached(self) -> bool: return mapping.from_maybe_impl(self._impl_obj.isDetached()) def addScriptTag( - self, url: str = None, path: str = None, content: str = None, type: str = None + self, + url: str = None, + path: typing.Union[str, pathlib.Path] = None, + content: str = None, + type: str = None, ) -> "ElementHandle": """Frame.addScriptTag @@ -2125,7 +2129,7 @@ def addScriptTag( ---------- url : Optional[str] URL of a script to be added. - path : Optional[str] + path : Union[str, pathlib.Path, NoneType] Path to the JavaScript file to be injected into frame. If `path` is a relative path, then it is resolved relative to current working directory. content : Optional[str] Raw JavaScript content to be injected into frame. @@ -2146,7 +2150,10 @@ def addScriptTag( ) def addStyleTag( - self, url: str = None, path: str = None, content: str = None + self, + url: str = None, + path: typing.Union[str, pathlib.Path] = None, + content: str = None, ) -> "ElementHandle": """Frame.addStyleTag @@ -2156,7 +2163,7 @@ def addStyleTag( ---------- url : Optional[str] URL of the `` tag. - path : Optional[str] + path : Union[str, pathlib.Path, NoneType] Path to the CSS file to be injected into frame. If `path` is a relative path, then it is resolved relative to current working directory. content : Optional[str] Raw CSS content to be injected into frame. @@ -2872,7 +2879,7 @@ def register( self, name: str, source: str = None, - path: str = None, + path: typing.Union[str, pathlib.Path] = None, contentScript: bool = None, ) -> NoneType: """Selectors.register @@ -3065,14 +3072,14 @@ def path(self) -> typing.Union[str, NoneType]: """ return mapping.from_maybe_impl(self._sync(self._impl_obj.path())) - def saveAs(self, path: typing.Union[pathlib.Path, str]) -> NoneType: + def saveAs(self, path: typing.Union[str, pathlib.Path]) -> NoneType: """Download.saveAs Saves the download to a user-specified path. Parameters ---------- - path : Union[pathlib.Path, str] + path : Union[str, pathlib.Path] Path where the download should be saved. """ return mapping.from_maybe_impl(self._sync(self._impl_obj.saveAs(path=path))) @@ -3537,7 +3544,11 @@ def evalOnSelectorAll( ) def addScriptTag( - self, url: str = None, path: str = None, content: str = None, type: str = None + self, + url: str = None, + path: typing.Union[str, pathlib.Path] = None, + content: str = None, + type: str = None, ) -> "ElementHandle": """Page.addScriptTag @@ -3548,7 +3559,7 @@ def addScriptTag( ---------- url : Optional[str] URL of a script to be added. - path : Optional[str] + path : Union[str, pathlib.Path, NoneType] Path to the JavaScript file to be injected into frame. If `path` is a relative path, then it is resolved relative to current working directory. content : Optional[str] Raw JavaScript content to be injected into frame. @@ -3569,7 +3580,10 @@ def addScriptTag( ) def addStyleTag( - self, url: str = None, path: str = None, content: str = None + self, + url: str = None, + path: typing.Union[str, pathlib.Path] = None, + content: str = None, ) -> "ElementHandle": """Page.addStyleTag @@ -3580,7 +3594,7 @@ def addStyleTag( ---------- url : Optional[str] URL of the `` tag. - path : Optional[str] + path : Union[str, pathlib.Path, NoneType] Path to the CSS file to be injected into frame. If `path` is a relative path, then it is resolved relative to current working directory. content : Optional[str] Raw CSS content to be injected into frame. @@ -4054,7 +4068,9 @@ def bringToFront(self) -> NoneType: """ return mapping.from_maybe_impl(self._sync(self._impl_obj.bringToFront())) - def addInitScript(self, source: str = None, path: str = None) -> NoneType: + def addInitScript( + self, source: str = None, path: typing.Union[str, pathlib.Path] = None + ) -> NoneType: """Page.addInitScript Adds a script which would be evaluated in one of the following scenarios: @@ -4134,7 +4150,7 @@ def screenshot( self, timeout: int = None, type: Literal["png", "jpeg"] = None, - path: str = None, + path: typing.Union[str, pathlib.Path] = None, quality: int = None, omitBackground: bool = None, fullPage: bool = None, @@ -4150,7 +4166,7 @@ def screenshot( Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the browserContext.setDefaultTimeout(timeout) or page.setDefaultTimeout(timeout) methods. type : Optional[Literal['png', 'jpeg']] Specify screenshot type, defaults to `png`. - path : Optional[str] + path : Union[str, pathlib.Path, NoneType] The file path to save the image to. The screenshot type will be inferred from file extension. If `path` is a relative path, then it is resolved relative to current working directory. If no path is provided, the image won't be saved to the disk. quality : Optional[int] The quality of the image, between 0-100. Not applicable to `png` images. @@ -4824,7 +4840,7 @@ def pdf( height: typing.Union[str, float] = None, preferCSSPageSize: bool = None, margin: PdfMargins = None, - path: str = None, + path: typing.Union[str, pathlib.Path] = None, ) -> bytes: """Page.pdf @@ -4898,7 +4914,7 @@ def pdf( Give any CSS `@page` size declared in the page priority over what is declared in `width` and `height` or `format` options. Defaults to `false`, which will scale the content to fit the paper size. margin : Optional[{"top": Union[str, int, NoneType], "right": Union[str, int, NoneType], "bottom": Union[str, int, NoneType], "left": Union[str, int, NoneType]}] Paper margins, defaults to none. - path : Optional[str] + path : Union[str, pathlib.Path, NoneType] The file path to save the PDF to. If `path` is a relative path, then it is resolved relative to current working directory. If no path is provided, the PDF won't be saved to the disk. Returns @@ -5230,7 +5246,9 @@ def setOffline(self, offline: bool) -> NoneType: self._sync(self._impl_obj.setOffline(offline=offline)) ) - def addInitScript(self, source: str = None, path: str = None) -> NoneType: + def addInitScript( + self, source: str = None, path: typing.Union[str, pathlib.Path] = None + ) -> NoneType: """BrowserContext.addInitScript Adds a script which would be evaluated in one of the following scenarios: @@ -5800,7 +5818,7 @@ def executablePath(self) -> str: def launch( self, - executablePath: str = None, + executablePath: typing.Union[str, pathlib.Path] = None, args: typing.List[str] = None, ignoreDefaultArgs: typing.Union[bool, typing.List[str]] = None, handleSIGINT: bool = None, @@ -5811,7 +5829,7 @@ def launch( headless: bool = None, devtools: bool = None, proxy: ProxyServer = None, - downloadsPath: str = None, + downloadsPath: typing.Union[str, pathlib.Path] = None, slowMo: int = None, chromiumSandbox: bool = None, ) -> "Browser": @@ -5826,7 +5844,7 @@ def launch( Parameters ---------- - executablePath : Optional[str] + executablePath : Union[str, pathlib.Path, NoneType] Path to a browser executable to run instead of the bundled one. If `executablePath` is a relative path, then it is resolved relative to current working directory. Note that Playwright only works with the bundled Chromium, Firefox or WebKit, use at your own risk. args : Optional[List[str]] Additional arguments to pass to the browser instance. The list of Chromium flags can be found here. @@ -5848,7 +5866,7 @@ def launch( **Chromium-only** Whether to auto-open a Developer Tools panel for each tab. If this option is `true`, the `headless` option will be set `false`. proxy : Optional[{"server": str, "bypass": Optional[str], "username": Optional[str], "password": Optional[str]}] Network proxy settings. - downloadsPath : Optional[str] + downloadsPath : Union[str, pathlib.Path, NoneType] If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed. slowMo : Optional[int] Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on. @@ -5883,7 +5901,7 @@ def launch( def launchServer( self, - executablePath: str = None, + executablePath: typing.Union[str, pathlib.Path] = None, args: typing.List[str] = None, ignoreDefaultArgs: typing.Union[bool, typing.List[str]] = None, handleSIGINT: bool = None, @@ -5894,7 +5912,7 @@ def launchServer( headless: bool = None, devtools: bool = None, proxy: ProxyServer = None, - downloadsPath: str = None, + downloadsPath: typing.Union[str, pathlib.Path] = None, port: int = None, chromiumSandbox: bool = None, ) -> "BrowserServer": @@ -5904,7 +5922,7 @@ def launchServer( Parameters ---------- - executablePath : Optional[str] + executablePath : Union[str, pathlib.Path, NoneType] Path to a browser executable to run instead of the bundled one. If `executablePath` is a relative path, then it is resolved relative to current working directory. **BEWARE**: Playwright is only guaranteed to work with the bundled Chromium, Firefox or WebKit, use at your own risk. args : Optional[List[str]] Additional arguments to pass to the browser instance. The list of Chromium flags can be found here. @@ -5926,7 +5944,7 @@ def launchServer( **Chromium-only** Whether to auto-open a Developer Tools panel for each tab. If this option is `true`, the `headless` option will be set `false`. proxy : Optional[{"server": str, "bypass": Optional[str], "username": Optional[str], "password": Optional[str]}] Network proxy settings. - downloadsPath : Optional[str] + downloadsPath : Union[str, pathlib.Path, NoneType] If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed. port : Optional[int] Port to use for the web socket. Defaults to 0 that picks any available port. @@ -5961,8 +5979,8 @@ def launchServer( def launchPersistentContext( self, - userDataDir: str, - executablePath: str = None, + userDataDir: typing.Union[str, pathlib.Path], + executablePath: typing.Union[str, pathlib.Path] = None, args: typing.List[str] = None, ignoreDefaultArgs: typing.Union[bool, typing.List[str]] = None, handleSIGINT: bool = None, @@ -5973,7 +5991,7 @@ def launchPersistentContext( headless: bool = None, devtools: bool = None, proxy: ProxyServer = None, - downloadsPath: str = None, + downloadsPath: typing.Union[str, pathlib.Path] = None, slowMo: int = None, viewport: IntSize = None, ignoreHTTPSErrors: bool = None, @@ -6000,9 +6018,9 @@ def launchPersistentContext( Parameters ---------- - userDataDir : str + userDataDir : Union[str, pathlib.Path] Path to a User Data Directory, which stores browser session data like cookies and local storage. More details for Chromium and Firefox. - executablePath : Optional[str] + executablePath : Union[str, pathlib.Path, NoneType] Path to a browser executable to run instead of the bundled one. If `executablePath` is a relative path, then it is resolved relative to current working directory. **BEWARE**: Playwright is only guaranteed to work with the bundled Chromium, Firefox or WebKit, use at your own risk. args : Optional[List[str]] Additional arguments to pass to the browser instance. The list of Chromium flags can be found here. @@ -6024,7 +6042,7 @@ def launchPersistentContext( **Chromium-only** Whether to auto-open a Developer Tools panel for each tab. If this option is `true`, the `headless` option will be set `false`. proxy : Optional[{"server": str, "bypass": Optional[str], "username": Optional[str], "password": Optional[str]}] Network proxy settings. - downloadsPath : Optional[str] + downloadsPath : Union[str, pathlib.Path, NoneType] If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed. slowMo : Optional[int] Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on. Defaults to 0.