VLearnVibium

How to Execute JavaScript in Vibium

Execute JavaScript in Vibium with vibe.evaluate(). Runnable Python examples for reading page values, returning data, and scrolling the page programmatically.

By Pramod Dutta··3 min read·Verified with Vibium 26.2
▶ Animated overview · made with Remotion

To execute JavaScript in Vibium, call vibe.evaluate() with a JavaScript expression as a string, and Vibium runs it in the page and returns the result to Python. Under the hood this maps to the WebDriver BiDi script.evaluate command, so the code runs in the real page context with full access to the DOM, window, and document. The expression's value is serialized and handed back as a native Python type — document.title returns a str, a number returns an int or float, and a returned object becomes a dict. This is the escape hatch for anything the high-level API doesn't directly expose: reading window.scrollY, pulling a value off a global object, scrolling by a pixel amount, or calling a function the page defines. For everyday clicking, typing, and reading you should still prefer find() and its actions, which auto-wait and read cleanly — but evaluate() is there when you need raw page access.

How do I run a JavaScript expression in Vibium?

Pass a JavaScript expression to vibe.evaluate() and Vibium executes it in the page, returning whatever the expression evaluates to. The simplest case reads a document property.

from vibium import browser_sync as browser
 
vibe = browser.launch()
vibe.go("https://example.com")
 
title = vibe.evaluate("document.title")
print(title)  # "Example Domain"
 
count = vibe.evaluate("document.querySelectorAll('a').length")
print(count)  # number of links on the page
 
vibe.quit()

The code runs in the page's own context, so anything the page can access — document, window, globals defined by site scripts — is available to your expression. The returned value is serialized back across the BiDi connection and arrives in Python as the matching type, so title is a string and count is an integer you can use directly.

How do I return data from the page?

evaluate() serializes the expression's result, so returning an array or object gives you a Python list or dictionary. This is the clean way to pull a batch of values in one round trip.

from vibium import browser_sync as browser
 
vibe = browser.launch()
vibe.go("https://example.com")
 
# Return an object — it arrives in Python as a dict.
info = vibe.evaluate("""
  ({
    title: document.title,
    url: location.href,
    links: document.querySelectorAll('a').length
  })
""")
 
print(info["title"], info["url"], info["links"])
 
vibe.quit()

Wrapping the object literal in parentheses keeps the JavaScript engine from reading the leading { as a block. Returning a structured object like this is far more efficient than five separate evaluate() calls — you collect everything in a single page-side execution and one serialized response.

How do I scroll the page with JavaScript?

Use evaluate() to call window.scrollTo or scrollBy when you need pixel-precise scrolling that the element API doesn't cover. This is common for triggering lazy-loaded content.

from vibium import browser_sync as browser
 
vibe = browser.launch()
vibe.go("https://example.com")
 
# Jump to the bottom of the page.
vibe.evaluate("window.scrollTo(0, document.body.scrollHeight)")
 
# Or scroll down by one viewport height.
vibe.evaluate("window.scrollBy(0, window.innerHeight)")
 
# Read how far down the page is now scrolled.
print(vibe.evaluate("window.scrollY"))
 
vibe.quit()

For scrolling a specific element into view before interacting, prefer el.scroll_into_view() on the element itself — it's more readable and ties into Vibium's actionability model. Use evaluate() scrolling when the goal is page-level movement, like driving an infinite-scroll feed.

When should I avoid evaluate()?

Don't reach for evaluate() to click buttons or type text — calling element.click() in JavaScript skips Vibium's actionability checks (visible, stable, receives events, enabled) and can fire on elements a real user could never reach, producing tests that pass against broken UIs. Use click() and the typed actions instead, and let Vibium auto-wait. Keep evaluate() for reading values, calling page functions, and low-level operations the high-level API genuinely doesn't expose.

Next steps

Frequently asked questions

How do I run JavaScript in Vibium?

Call vibe.evaluate() with a JavaScript expression as a string. Vibium runs it in the page context through the WebDriver BiDi script.evaluate command and returns the serialized result to Python, so you can read DOM values, document properties, or any computed value back into your script.

Can vibe.evaluate() return a value to Python?

Yes. The result of the JavaScript expression is serialized and returned by evaluate(). Strings, numbers, booleans, arrays, and plain objects come back as the matching Python types, so document.title returns a string and a returned object becomes a dictionary you can index.

When should I use evaluate() instead of find()?

Prefer find() and its actions for clicking, typing, and reading elements, because they auto-wait and stay readable. Reach for evaluate() when you need something the high-level API does not cover, such as reading window properties, calling a page function, or scrolling by a pixel amount.

Vibium is created by Jason Huggins. This is an independent tutorial — see the official Vibium site and GitHub repo for canonical docs.

Related guides