pub struct Selectors { /* private fields */ }Expand description
Selectors — manages custom selector engines and test ID attribute configuration.
An instance of Selectors is available via crate::protocol::Playwright::selectors().
Selector engines registered here are applied to all browser contexts. Register engines before creating pages that will use them.
Implementations§
Source§impl Selectors
impl Selectors
Sourcepub async fn add_context(&self, channel: Channel) -> Result<()>
pub async fn add_context(&self, channel: Channel) -> Result<()>
Registers a context’s channel so it receives selector updates.
Called by BrowserContext when it is created, so that:
- All previously registered engines are applied to it immediately.
- Future
register()/set_test_id_attribute()calls reach it.
Sourcepub fn remove_context(&self, channel: &Channel)
pub fn remove_context(&self, channel: &Channel)
Removes a context’s channel when it is closed.
Called by BrowserContext on close to avoid sending messages to dead channels.
Sourcepub async fn register(
&self,
name: &str,
script: &str,
content_script: Option<bool>,
) -> Result<()>
pub async fn register( &self, name: &str, script: &str, content_script: Option<bool>, ) -> Result<()>
Registers a custom selector engine.
The script must evaluate to an object with query and queryAll methods:
{
query(root, selector) { return root.querySelector(selector); },
queryAll(root, selector) { return Array.from(root.querySelectorAll(selector)); }
}After registration, use the engine with page.locator("name=selector").
§Arguments
name- Name to assign to the selector engine.script- JavaScript string that evaluates to a selector engine factory.content_script- Whether to run the engine in isolated content script mode. Defaults tofalse.
§Errors
Returns error if:
- A selector engine with the same name is already registered
- Any context rejects the registration (invalid script, etc.)
See: https://playwright.dev/docs/api/class-selectors#selectors-register
Sourcepub fn test_id_attribute(&self) -> String
pub fn test_id_attribute(&self) -> String
Returns the current test ID attribute name used by get_by_test_id() locators.
Defaults to "data-testid".
Sourcepub async fn set_test_id_attribute(&self, attribute: &str) -> Result<()>
pub async fn set_test_id_attribute(&self, attribute: &str) -> Result<()>
Sets the attribute used by get_by_test_id() locators.
By default, Playwright uses data-testid. Calling this method changes the
attribute name for all current and future contexts.
§Arguments
attribute- The attribute name to use as the test ID (e.g.,"data-custom-id").
§Errors
Returns error if any context rejects the update.
See: https://playwright.dev/docs/api/class-selectors#selectors-set-test-id-attribute