I would like to
However, puppeteer code is server side node.js code and not originally thought to be executed in a browser.
Instead of server side code:
const puppeteer = require('puppeteer'); // v20.7.4 or later
(async () => {
const browser = await puppeteer.launch({headless: 'new'});
const page = await browser.newPage();
//...
I would like to use some client side code like:
//load some puppeteer wrapper from cdn
fetch('https://cdn.jsdelivr.net/.../web-puppeteer.min.js')
.then(response => response.text())
.then(text => eval(text))
//connect to current browser session using that wrapper
const page = webPuppeteer.connectToCurrentPage(document);
//use previously recorded puppeteer commands for browser automation
//...
webPuppeteer.Locator.race([
page.locator('::-p-aria(Search)'),
page.locator('#APjFqb'),
page.locator('::-p-xpath(//*[@id=\\"APjFqb\\"])'),
page.locator(':scope >>> #APjFqb')
])
.fill('foo');
I found puppeteer-web. However, that project is archived and it seems that it would still need an extra server:
https://github.com/entrptaher/puppeteer-web
It also seems to be possible to start google chrome with the option --remote-debugging-port
. However, that does not seem to make sense for my use case.
=> How to use puppeteer commands client side for the current browser session?
Of course not all the puppeteer commands would be supported. I only would need the ones related to the current page.
=> If that does not work with puppeteer code, is there another way to replay json that has been recorded with google chrome Recorder in client side Snippets?
=> Or is there a way to edit the code in the recorder to include conditional statements etc? The steps of puppeteer replay do not seem to include a "loop" or "if" step.
Related:
Connecting Browsers in Puppeteer
https://github.com/entrptaher/puppeteer-web
How to use puppeteer in my website with a cdn
How to run Puppeteer code in any web browser?
How to make Puppeteer work with a ReactJS application on the client-side
Is There Any Client Side Web Automation
Flows that has been recorded with chrome dev tools can be shown as json: Recorder => Show code => JSON.
Copy that JSON to vanilla JavaScript Snippet and write a custom implementation to process the flow as wanted.
For example, in order to process recorded button clicks:
var json = {
"title": "foo",
"steps": [
{
"type": "click",
"target": "main",
"selectors": [
[
"aria/ Ordner "
],
[
"#rcmbtn106"
],
[
"xpath///*[@id=\"rcmbtn106\"]"
],
[
"pierce/#rcmbtn106"
]
],
}
]
};
for (var step in json.steps){
if (step.type == 'click'){
var selector = step.selectors[1][0];
var targetElement = document.querySelector(selector);
targetElement.click();
}
// implement other step types here
}