DefaultTab

open class DefaultTab(websocketUrl: String, messageListeningScope: CoroutineScope, eventsBufferSize: Int, var targetInfo: Target.TargetInfo, var owner: Browser? = null) : DefaultConnection, Tab

Represents a browser tab, which is a connection to a specific target in the browser.

This class provides methods to interact with the tab, such as navigating to URLs, managing history, evaluating JavaScript expressions, and manipulating the DOM.

Constructors

Link copied to clipboard
constructor(websocketUrl: String, messageListeningScope: CoroutineScope, eventsBufferSize: Int, targetInfo: Target.TargetInfo, owner: Browser? = null)

Properties

Link copied to clipboard
open override val events: Flow<Message.Event>
Link copied to clipboard
open override val generatedDomains: MutableMap<KClass<out Domain>, Domain>
Link copied to clipboard
Link copied to clipboard
open override val responses: Flow<Message.Response>
Link copied to clipboard
open val targetId: String?
Link copied to clipboard
open override var targetInfo: Target.TargetInfo?
Link copied to clipboard
open val type: String?

Functions

Link copied to clipboard
open suspend override fun activate()

Activates the tab, bringing it to the foreground.

Link copied to clipboard
inline fun <T> Connection.addHandler(coroutineScope: CoroutineScope, crossinline event: CDP.() -> Flow<T>, crossinline handler: suspend (T) -> Unit): Job

Adds a handler for a specific CDP event.

Link copied to clipboard
open suspend override fun back()

Navigate back in the tab's history.

Link copied to clipboard
open suspend override fun bringToFront()

Brings the tab to the front, activating it.

Link copied to clipboard
open suspend override fun callCommand(method: String, parameter: JsonElement?, mode: CommandMode): JsonElement?

Internal method to call a CDP command.

Link copied to clipboard
open suspend override fun close()

Closes the websocket connection. Should not be called manually by users.

Link copied to clipboard
open suspend override fun disableDomAgent()

Disables the DOM agent to stop receiving DOM-related events.

Link copied to clipboard
inline suspend fun <T> Tab.evaluate(expression: String, awaitPromise: Boolean = false): T?

Evaluates a JavaScript expression in the context of the tab.

Link copied to clipboard
open suspend override fun <T> expect(urlPattern: Regex, block: suspend RequestExpectation.() -> T): T

Expects a request to match the given urlPattern.

Link copied to clipboard
open suspend override fun find(text: String, bestMatch: Boolean, returnEnclosingElement: Boolean, timeout: Long): Element

Finds a single element by its text content, optionally waiting for it to appear.

Link copied to clipboard
open suspend override fun findAll(text: String, timeout: Long): List<Element>

Finds multiple elements by their text content, optionally waiting for them to appear.

Link copied to clipboard
open suspend override fun findElementByText(text: String, bestMatch: Boolean, returnEnclosingElement: Boolean): Element?

Finds and returns the first element containing the specified text, or the best match if bestMatch is true.

Link copied to clipboard
open suspend override fun findElementsByText(text: String, tagHint: String?): List<Element>

Returns elements which match the given text. Please note: this may (or will) also return any other element (like inline scripts), which happen to contain that text.

Link copied to clipboard
open suspend override fun forward()

Navigate forward in the tab's history.

Link copied to clipboard
open suspend override fun fullscreen()

Sets the tab's window to fullscreen mode.

Link copied to clipboard
open suspend override fun get(url: String, newTab: Boolean, newWindow: Boolean): Tab

Navigate to a specified URL in the tab.

Link copied to clipboard
open suspend override fun getAllLinkedSources(): List<Element>

Gets all elements of tag: link, a, img, script, meta.

Link copied to clipboard
open suspend override fun getAllUrls(absolute: Boolean): List<String>

Convenience function, which returns all links (a, link, img, script, meta).

Link copied to clipboard
open suspend override fun getContent(): String

Gets the content of the tab as a string.

Link copied to clipboard
open suspend override fun getWindow(): Browser.GetWindowForTargetReturn

Gets the current window information for the tab.

Link copied to clipboard
open suspend override fun <T> intercept(urlPattern: String, requestStage: Fetch.RequestStage, resourceType: Network.ResourceType, block: suspend FetchInterception.() -> T): T

Intercepts network requests matching the given urlPattern and requestStage. This allows you to modify requests, responses, or block them entirely.

Link copied to clipboard
open suspend override fun maximize()

Maximizes the tab's window.

Link copied to clipboard
open suspend override fun medimize()

Restores the tab's window to its normal state.

Link copied to clipboard
open suspend override fun minimize()

Minimizes the tab's window.

Link copied to clipboard
open suspend override fun mouseClick(x: Double, y: Double, button: Input.MouseButton, buttons: Int, modifiers: Int)

Performs a native mouse click at the specified \[x, y\] coordinates.

Link copied to clipboard
open suspend override fun mouseMove(x: Double, y: Double, steps: Int, flash: Boolean)

Moves the mouse cursor to the specified \[x, y\] coordinates, optionally in multiple steps and with a flash effect.

Link copied to clipboard
open suspend override fun querySelector(selector: String, node: NodeOrElement?): Element?

Finds a single element based on a CSS selector string.

Link copied to clipboard
open suspend override fun querySelectorAll(selector: String, node: NodeOrElement?): List<Element>

Equivalent of JavaScript's document.querySelectorAll. This is considered one of the main methods to use in this package.

Link copied to clipboard
open suspend override fun rawEvaluate(expression: String, awaitPromise: Boolean): JsonElement?

Evaluates a JavaScript expression in the context of the tab.

Link copied to clipboard
open suspend override fun reload(ignoreCache: Boolean, scriptToEvaluateOnLoad: String?)

Reload the current page in the tab.

Link copied to clipboard
open suspend override fun saveScreenshot(filename: Path?, format: ScreenshotFormat, fullPage: Boolean): String

Saves a screenshot of the page. This is not the same as Element.saveScreenshot, which saves a screenshot of a single element only.

Link copied to clipboard
open suspend override fun screenshotB64(format: ScreenshotFormat, fullPage: Boolean): String

Takes a screenshot of the page and returns the result as a base64 encoded string. This is not the same as Element.screenshotB64, which takes a screenshot of a single element only.

Link copied to clipboard
open suspend override fun scrollDown(amount: Int, speed: Int)

Scrolls the tab down by a specified amount.

Link copied to clipboard
open suspend override fun scrollUp(amount: Int, speed: Int)

Scrolls the tab up by a specified amount.

Link copied to clipboard
open suspend override fun select(selector: String, timeout: Long): Element

Selects an element in the DOM using a CSS selector.

Link copied to clipboard
open suspend override fun selectAll(selector: String, timeout: Long, includeFrames: Boolean): List<Element>

Finds multiple elements by CSS selector, optionally waiting for them to appear.

Link copied to clipboard
inline fun <T> Connection.send(command: CDP.() -> T): T

Sends a CDP command and waits for the response.

Link copied to clipboard
open suspend override fun setUserAgent(userAgent: String?, acceptLanguage: String?, platform: String?)

Sets the user agent for the tab.

Link copied to clipboard
open suspend override fun setWindowState(left: Int, top: Int, width: Int, height: Int, state: String)

Sets the tab's window state, including position and size.

Link copied to clipboard
open suspend override fun sleep(t: Long)

Suspends the coroutine for a specified time in milliseconds.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
open suspend override fun updateTarget()

Updates the target information by fetching it from the CDP.

Link copied to clipboard
open suspend override fun wait(t: Long?)

Waits until the event listener reports idle (no new events received in a certain timespan). When \`t\` is provided, ensures waiting for \`t\` milliseconds, no matter what.

Link copied to clipboard
open suspend override fun waitForReadyState(until: ReadyState, timeout: Long): Boolean

Waits for the document's ready state to reach a specified state.

Link copied to clipboard
open suspend override fun xpath(xpath: String, timeout: Long): List<Element>

Finds elements by XPath string.