Class: Playwright::Selectors

Inherits:
PlaywrightApi show all
Defined in:
lib/playwright_api/selectors.rb

Overview

Selectors can be used to install custom selector engines. See [Working with selectors](./selectors.md#working-with-selectors) for more information.

Instance Method Summary collapse

Methods inherited from PlaywrightApi

#==, from_channel_owner, #initialize

Constructor Details

This class inherits a constructor from Playwright::PlaywrightApi

Instance Method Details

#off(event, callback) ⇒ Object

– inherited from EventEmitter –



63
64
65
# File 'lib/playwright_api/selectors.rb', line 63

def off(event, callback)
  wrap_impl(@impl.off(event, callback))
end

#on(event, callback) ⇒ Object

– inherited from EventEmitter –



57
58
59
# File 'lib/playwright_api/selectors.rb', line 57

def on(event, callback)
  wrap_impl(@impl.on(event, callback))
end

#once(event, callback) ⇒ Object

– inherited from EventEmitter –



69
70
71
# File 'lib/playwright_api/selectors.rb', line 69

def once(event, callback)
  wrap_impl(@impl.once(event, callback))
end

#register(name, script, contentScript: nil) ⇒ Object

An example of registering selector engine that queries elements based on a tag name:

“‘js const { selectors, firefox } = require(’playwright’); // Or ‘chromium’ or ‘webkit’.

(async () => {

// Must be a function that evaluates to a selector engine instance.
const createTagNameEngine = () => ({
  // Returns the first element matching given selector in the root's subtree.
  query(root, selector) {
    return root.querySelector(selector);
  },

  // Returns all elements matching given selector in the root's subtree.
  queryAll(root, selector) {
    return Array.from(root.querySelectorAll(selector));
  }
});

// Register the engine. Selectors will be prefixed with "tag=".
await selectors.register('tag', createTagNameEngine);

const browser = await firefox.launch();
const page = await browser.newPage();
await page.setContent(`<div><button>Click me</button></div>`);

// Use the selector prefixed with its name.
const button = await page.$('tag=button');
// Combine it with other selector engines.
await page.click('tag=div >> text="Click me"');
// Can use it in any methods supporting selectors.
const buttonCount = await page.$$eval('tag=button', buttons => buttons.length);

await browser.close();

})(); “‘

“‘python async # FIXME: add snippet “`

“‘python sync # FIXME: add snippet “`

Raises:

  • (NotImplementedError)


51
52
53
# File 'lib/playwright_api/selectors.rb', line 51

def register(name, script, contentScript: nil)
  raise NotImplementedError.new('register is not implemented yet.')
end