Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,36 +87,37 @@ cannot serve MCP and CDP from the same one). The package itself needs only
Python's standard library; Playwright is a dev-only test dependency and
`connect_over_cdp` never downloads a browser.

## Drive it with Selenium (WebDriver BiDi)
## Drive it with Selenium (WebDriver)

The browser also speaks [WebDriver BiDi](https://w3c.github.io/webdriver-bidi/).
`BiDiServer` starts `lightpanda serve --protocol webdriver` and hands you the
URL Selenium's `webdriver.Remote` takes as `command_executor`:
The browser also speaks [WebDriver](https://www.w3.org/TR/webdriver2/), both
classic and [BiDi](https://w3c.github.io/webdriver-bidi/). `WebDriverServer`
starts `lightpanda serve --protocol webdriver` and hands you the URL
Selenium's `webdriver.Remote` takes as `command_executor`, so no chromedriver
is involved:

```python
from lightpanda import BiDiServer
from lightpanda import WebDriverServer
from selenium import webdriver
from selenium.webdriver.common.options import ArgOptions
from selenium.webdriver.common.by import By

options = ArgOptions()
options.web_socket_url = True # ask for a WebDriver BiDi session

with BiDiServer() as server:
driver = webdriver.Remote(command_executor=server.http_endpoint, options=options)
context = driver.browsing_context.create(type="tab")
driver.browsing_context.navigate(context=context, url="https://example.com", wait="complete")
print(driver.script.execute("() => document.title", context_id=context)["value"])
with WebDriverServer() as server:
# Selenium requires an options object; Lightpanda accepts any browser's.
driver = webdriver.Remote(command_executor=server.http_endpoint, options=webdriver.ChromeOptions())
driver.get("https://example.com")
print(driver.find_element(By.CSS_SELECTOR, "h1").text)
driver.quit()
```

`AsyncBiDiServer` is the asyncio twin, and `server.bidi_endpoint`
In a pytest suite, start the server once in a session-scoped fixture and open
a driver per test; with pytest-xdist each worker gets its own server on its
own free port.

Set `options.web_socket_url = True` to also get a WebDriver BiDi session and
use `driver.browsing_context` / `driver.script`. `server.bidi_endpoint`
(`ws://127.0.0.1:<port>/session`) is the raw BiDi WebSocket for clients that
speak the protocol directly. The browser serves the BiDi modules plus the
classic session bootstrap Selenium needs, not the classic WebDriver
commands: `driver.get`, `find_element` and friends are not implemented, so
drive the page through `driver.browsing_context` and `driver.script` with an
explicit context, created first as above. Pass `args=["--protocol", "cdp"]`
to serve CDP on the same port as well.
speak the protocol directly. `WebDriverServer` is an alias of `BiDiServer`,
`AsyncWebDriverServer` (alias of `AsyncBiDiServer`) is the asyncio twin, and
`args=["--protocol", "cdp"]` serves CDP on the same port as well.

## Respecting robots.txt

Expand Down
9 changes: 6 additions & 3 deletions lightpanda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@

For Playwright or Puppeteer code, ``CDPServer`` runs the browser's own
Chrome DevTools Protocol server and hands you the endpoint to connect to
(see its docs for an example). For Selenium, ``BiDiServer`` serves WebDriver
BiDi the same way and hands you the ``command_executor`` URL.
(see its docs for an example). For Selenium, ``BiDiServer`` (alias
``WebDriverServer``) serves WebDriver, classic and BiDi, the same way and
hands you the ``command_executor`` URL.

The browser can enforce ``robots.txt``, which is off by default. Pass
``args=["--obey-robots"]`` to any of these and a disallowed request fails
Expand All @@ -36,7 +37,7 @@
__docformat__ = "google"

from .async_browser import AsyncBrowser, AsyncSession, run_script_async
from .bidi import AsyncBiDiServer, BiDiServer
from .bidi import AsyncBiDiServer, AsyncWebDriverServer, BiDiServer, WebDriverServer
from .browser import Browser, PageResult, Session, run_script
from .cdp import AsyncCDPServer, CDPServer
from .errors import LightpandaError, ProcessError, ProtocolError, ScriptError, ToolError
Expand All @@ -53,6 +54,8 @@
"AsyncCDPServer",
"BiDiServer",
"AsyncBiDiServer",
"WebDriverServer",
"AsyncWebDriverServer",
"LightpandaError",
"ProcessError",
"ProtocolError",
Expand Down
Loading
Loading