public interface BrowserType
import com.microsoft.playwright.*;
public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
BrowserType chromium = playwright.chromium();
Browser browser = chromium.launch();
Page page = browser.newPage();
page.navigate("https://example.com");
// other actions...
browser.close();
}
}
}
Modifier and Type | Interface and Description |
---|---|
static class |
BrowserType.ConnectOptions |
static class |
BrowserType.ConnectOverCDPOptions |
static class |
BrowserType.LaunchOptions |
static class |
BrowserType.LaunchPersistentContextOptions |
Modifier and Type | Method and Description |
---|---|
default Browser |
connect(String wsEndpoint)
This method attaches Playwright to an existing browser instance.
|
Browser |
connect(String wsEndpoint,
BrowserType.ConnectOptions options)
This method attaches Playwright to an existing browser instance.
|
default Browser |
connectOverCDP(String endpointURL)
This method attaches Playwright to an existing browser instance using the Chrome DevTools Protocol.
|
Browser |
connectOverCDP(String endpointURL,
BrowserType.ConnectOverCDPOptions options)
This method attaches Playwright to an existing browser instance using the Chrome DevTools Protocol.
|
String |
executablePath()
A path where Playwright expects to find a bundled browser executable.
|
default Browser |
launch()
Returns the browser instance.
|
Browser |
launch(BrowserType.LaunchOptions options)
Returns the browser instance.
|
default BrowserContext |
launchPersistentContext(Path userDataDir)
Returns the persistent browser context instance.
|
BrowserContext |
launchPersistentContext(Path userDataDir,
BrowserType.LaunchPersistentContextOptions options)
Returns the persistent browser context instance.
|
String |
name()
Returns browser name.
|
default Browser connect(String wsEndpoint)
BrowserType.launchServer
in Node.js, the major and minor version needs to match the client version (1.2.3 → is
compatible with 1.2.x).wsEndpoint
- A browser websocket endpoint to connect to.Browser connect(String wsEndpoint, BrowserType.ConnectOptions options)
BrowserType.launchServer
in Node.js, the major and minor version needs to match the client version (1.2.3 → is
compatible with 1.2.x).wsEndpoint
- A browser websocket endpoint to connect to.default Browser connectOverCDP(String endpointURL)
The default browser context is accessible via Browser.contexts()
.
NOTE: Connecting over the Chrome DevTools Protocol is only supported for Chromium-based browsers.
Browser browser = playwright.chromium().connectOverCDP("http://localhost:9222");
BrowserContext defaultContext = browser.contexts().get(0);
Page page = defaultContext.pages().get(0);
endpointURL
- A CDP websocket endpoint or http url to connect to. For example http://localhost:9222/
or
ws://127.0.0.1:9222/devtools/browser/387adf4c-243f-4051-a181-46798f4a46f4
.Browser connectOverCDP(String endpointURL, BrowserType.ConnectOverCDPOptions options)
The default browser context is accessible via Browser.contexts()
.
NOTE: Connecting over the Chrome DevTools Protocol is only supported for Chromium-based browsers.
Browser browser = playwright.chromium().connectOverCDP("http://localhost:9222");
BrowserContext defaultContext = browser.contexts().get(0);
Page page = defaultContext.pages().get(0);
endpointURL
- A CDP websocket endpoint or http url to connect to. For example http://localhost:9222/
or
ws://127.0.0.1:9222/devtools/browser/387adf4c-243f-4051-a181-46798f4a46f4
.String executablePath()
default Browser launch()
You can use ignoreDefaultArgs
to filter out --mute-audio
from default arguments:
// Or "firefox" or "webkit".
Browser browser = chromium.launch(new BrowserType.LaunchOptions()
.setIgnoreDefaultArgs(Arrays.asList("--mute-audio")));
> **Chromium-only** Playwright can also be used to control the Google Chrome or Microsoft Edge browsers, but it works
best with the version of Chromium it is bundled with. There is no guarantee it will work with any other version. Use
executablePath
option with extreme caution.
>
> If Google Chrome (rather than Chromium) is preferred, a Chrome Canary or Dev Channel build is suggested.
>
> Stock browsers like Google Chrome and Microsoft Edge are suitable for tests that require proprietary media codecs for video playback. See this article for other differences between Chromium and Chrome. This article describes some differences for Linux users.
Browser launch(BrowserType.LaunchOptions options)
You can use ignoreDefaultArgs
to filter out --mute-audio
from default arguments:
// Or "firefox" or "webkit".
Browser browser = chromium.launch(new BrowserType.LaunchOptions()
.setIgnoreDefaultArgs(Arrays.asList("--mute-audio")));
> **Chromium-only** Playwright can also be used to control the Google Chrome or Microsoft Edge browsers, but it works
best with the version of Chromium it is bundled with. There is no guarantee it will work with any other version. Use
executablePath
option with extreme caution.
>
> If Google Chrome (rather than Chromium) is preferred, a Chrome Canary or Dev Channel build is suggested.
>
> Stock browsers like Google Chrome and Microsoft Edge are suitable for tests that require proprietary media codecs for video playback. See this article for other differences between Chromium and Chrome. This article describes some differences for Linux users.
default BrowserContext launchPersistentContext(Path userDataDir)
Launches browser that uses persistent storage located at userDataDir
and returns the only context. Closing this
context will automatically close the browser.
userDataDir
- Path to a User Data Directory, which stores browser session data like cookies and local storage. More details for Chromium and Firefox. Note that
Chromium's user data directory is the **parent** directory of the "Profile Path" seen at chrome://version
. Pass an
empty string to use a temporary directory instead.BrowserContext launchPersistentContext(Path userDataDir, BrowserType.LaunchPersistentContextOptions options)
Launches browser that uses persistent storage located at userDataDir
and returns the only context. Closing this
context will automatically close the browser.
userDataDir
- Path to a User Data Directory, which stores browser session data like cookies and local storage. More details for Chromium and Firefox. Note that
Chromium's user data directory is the **parent** directory of the "Profile Path" seen at chrome://version
. Pass an
empty string to use a temporary directory instead.String name()
"chromium"
, "webkit"
or "firefox"
.Copyright © 2022. All rights reserved.