Skip to content

WebView Interaction Tools

Comprehensive tools for interacting with your Tauri application's webview, including gestures, keyboard input, screenshots, and JavaScript execution.

tauri_webview_interact

Perform various interaction gestures on webview elements.

Parameters

NameTypeRequiredDescription
actionstringYesAction: 'click', 'double-click', 'long-press', 'scroll', 'swipe', 'focus'
selectorstringNoCSS selector for the element to interact with
xnumberNoX coordinate for direct coordinate interaction
ynumberNoY coordinate for direct coordinate interaction
durationnumberNoDuration in ms (default: 500ms for long-press, 300ms for swipe)
scrollXnumberNoHorizontal scroll amount in pixels (positive = right)
scrollYnumberNoVertical scroll amount in pixels (positive = down)
fromXnumberNoStarting X coordinate for swipe
fromYnumberNoStarting Y coordinate for swipe
toXnumberNoEnding X coordinate for swipe
toYnumberNoEnding Y coordinate for swipe

Example

javascript
// Click an element by selector
{
  "tool": "tauri_webview_interact",
  "action": "click",
  "selector": "#submit-button"
}

// Long press at coordinates
{
  "tool": "tauri_webview_interact",
  "action": "long-press",
  "x": 100,
  "y": 200,
  "duration": 1000
}

// Scroll an element
{
  "tool": "tauri_webview_interact",
  "action": "scroll",
  "selector": ".content-area",
  "scrollY": 200
}

// Swipe gesture
{
  "tool": "tauri_webview_interact",
  "action": "swipe",
  "fromX": 200,
  "fromY": 100,
  "toX": 200,
  "toY": 400,
  "duration": 500
}

// Focus an element
{
  "tool": "tauri_webview_interact",
  "action": "focus",
  "selector": "#username-input"
}

tauri_webview_screenshot

Capture a screenshot of the current viewport (visible area) of the webview.

Parameters

NameTypeRequiredDescription
formatstringNoImage format: 'png', 'jpeg' (default: 'png')
qualitynumberNoJPEG quality 0-100 (only for jpeg format)
filePathstringNoFile path to save the screenshot to instead of returning base64
windowIdstringNoWindow label to target (defaults to 'main')

Example

javascript
// Take a PNG screenshot (returns base64)
{
  "tool": "tauri_webview_screenshot",
  "format": "png"
}

// Save screenshot to a file
{
  "tool": "tauri_webview_screenshot",
  "format": "png",
  "filePath": "/path/to/screenshot.png"
}

Response

Returns a base64-encoded image, or if filePath is provided, returns the path where the screenshot was saved.

TIP

This only captures what is currently visible. Scroll content into view before taking screenshots if you need to capture specific elements.

tauri_webview_keyboard

Type text or send keyboard events to the webview.

Parameters

NameTypeRequiredDescription
actionstringYesAction: 'type', 'press', 'down', 'up'
selectorstringNoCSS selector for element to type into (required for 'type' action)
textstringNoText to type (required for 'type' action)
keystringNoKey to press (required for 'press/down/up' actions, e.g., 'Enter', 'Escape')
modifiersstring[]NoModifier keys: ['Control', 'Alt', 'Shift', 'Meta']

Example

javascript
// Type text into an input
{
  "tool": "tauri_webview_keyboard",
  "action": "type",
  "selector": "#username",
  "text": "Hello World"
}

// Send keyboard shortcut
{
  "tool": "tauri_webview_keyboard",
  "action": "press",
  "key": "s",
  "modifiers": ["Control"]
}

tauri_webview_wait_for

Wait for specific conditions in the webview.

Parameters

NameTypeRequiredDescription
typestringYesWhat to wait for: 'selector', 'text', 'ipc-event'
valuestringYesSelector, text content, or IPC event name to wait for
timeoutnumberNoTimeout in milliseconds (default: 5000ms)

Example

javascript
// Wait for element to appear
{
  "tool": "tauri_webview_wait_for",
  "type": "selector",
  "value": "#loading-complete",
  "timeout": 10000
}

// Wait for text to appear
{
  "tool": "tauri_webview_wait_for",
  "type": "text",
  "value": "Success!"
}

tauri_webview_execute_js

Execute JavaScript code in the webview context.

Parameters

NameTypeRequiredDescription
scriptstringYesJavaScript code to execute
argsarrayNoArguments to pass to the script

Script Format

Scripts can be any valid JavaScript. If you need a return value, it must be JSON-serializable:

  • Side effects only: console.log('hello'), document.body.classList.add('dark')
  • Simple expressions: document.title, 5 + 3, window.location.href
  • Statements with return: const x = 5; return x * 2;
  • Async operations: const res = await fetch('/api'); return await res.json();
  • IIFE (Immediately Invoked Function Expression): (() => { return 5; })()

Returning Values from Functions

If you want to return a value from a function, use an IIFE: (() => { return 5; })() not () => { return 5; }. Bare function definitions are not JSON-serializable and will return null.

Example

javascript
// Get page data (simple expression)
{
  "tool": "tauri_webview_execute_js",
  "script": "document.title + ' - ' + window.location.href"
}

// Async operation
{
  "tool": "tauri_webview_execute_js",
  "script": "const res = await fetch('/api/data'); return await res.json();"
}

// IIFE for complex logic
{
  "tool": "tauri_webview_execute_js",
  "script": "(() => { const items = document.querySelectorAll('li'); return items.length; })()"
}

Response

Returns the result of the JavaScript execution as a JSON string. Non-serializable values (like functions or DOM elements) will return null.

tauri_webview_get_styles

Get computed CSS styles for elements.

Parameters

NameTypeRequiredDescription
selectorstringYesCSS selector for element(s) to get styles from
propertiesstring[]NoSpecific CSS properties to retrieve (if omitted, returns all)
multiplebooleanNoGet styles for all matching elements (default: false)

Example

javascript
// Get specific styles
{
  "tool": "tauri_webview_get_styles",
  "selector": "#my-element",
  "properties": ["color", "background-color", "font-size"]
}

Response

Returns a JSON string with the computed styles.

Common Patterns

Dismissing the Keyboard

To dismiss the on-screen keyboard, use tauri_webview_execute_js:

javascript
{
  "tool": "tauri_webview_execute_js",
  "script": "document.activeElement?.blur()"
}

This is an unofficial community project. Not affiliated with, endorsed by, or associated with the Tauri project or CrabNebula Ltd.