One possible issue you are having is that the click event has isTrusted false.
If you plan on doing a lot of similar stuff I would suggest you switch to using puppeteer as is done by basically all simulational QA.
this google search might help
Another issue you could be having is that most Web Apps these days have a shadow DOM in a walled garden that you are not allowed to interact with. This DOM is usually the interior source of truth for the app, and on interaction it replaces the shallow surface you see in the dev tools.
I recently started automating a lot of simple tasks with puppeteer after tampermonkey started failing me similarly, here are some pointers to get You started if you`d like. It's kind of a commitment to learning, but it quickly becomes vastly superior to anything else.
after installing node & running in node.js in a file run using cmd or whatever (i use git bash)
//Edit
npm init -y
npm i -D child_process path puppeteer-core
child-process is to spawn the puppet browser
I'd add dotenv if you can code much.
Edit\\
node filename.js
import puppeteer from "puppeteer-core";
import cp from "child_process";
import path from "path";
// or just hard code an OS path to chrome.exe here is mine
const user_dir = path.resolve("C:\\\\", "Users", "%REPLACE_USER%");
const ad_local = path.join(user_dir, "AppData", "Local");
const chrome = path.join(ad_local, "Chromium", "Application", "chrome_proxy.exe");
let http;
// required for puppeteer to talk to pre-opened browser
let wsChromeEndpointUrl;
// https://stackoverflow.com/questions/51200626/using-a-settimeout-in-a-async-function
const wait = (delay = 300) => new Promise((resolve) => setTimeout(resolve, delay));
const _l = (s) => {
console.log(s);
};
// this opens browser with an open socket you can drive from nodejs
// don't use it for ANY secure login such as banking or email
// --user-data-dir is a dir for node/browser to peek/share
// opening some other browser after you can see wsChromeEndpointUrl listed there, careful yeah!
// http://127.0.0.1:9222/json/version
// edit: `start chrome.exe` is good for windows, you can use cp.execFile("chrome.exe...") fairly directly otherwise. You get hangs in windows on Ctrl-C without `start...`
const open_browser = async () => {
_l(`${chrome} --remote-debugging-port=9222 --user-data-dir=remote-profile`);
await wait(2500);
try {
cp.exec(
`start ${chrome} --remote-debugging-port=9222 --user-data-dir=remote-profile ${landing}`,
);
} catch (err) {
_l(`L:175, F:server_proc.js open_browser err: ${JSON.stringify(err)}`);
}
await wait(2500);
};
(async () => {
try {
// console.log("http?");
http = await import("http");
} catch (err) {
_l("[075] puppers.js - http support is disabled!");
return;
}
await connect_browser();
if (!wsChromeEndpointUrl) {
_l("[161] main.js - browser failed to initialize, terminating");
return;
}
await now_you_can_drive(wsChromeEndpointUrl);
})();
// 127.0.0.1
// aka localhost
const try_connect_browser = () => {
return new Promise((resolve, reject) => {
const options = {
hostname: "127.0.0.1",
port: 9222,
path: "/json/version",
method: "GET",
};
const req_ = http.request(options, async (res_) => {
await res_.on("data", async (d) => {
if (d) {
wsChromeEndpointUrl = JSON.parse(d)["webSocketDebuggerUrl"];
resolve(true);
}
});
});
req_.on("error", async (err) => {
// console.error(err);
_l("[110] puppers.js - chrome proxy err = err: ");
_l(err);
reject();
});
req_.end();
});
};
const connect_browser = async (retries_remaining = 1) => {
await try_connect_browser()
.then(async (res) => {})
.catch(async (err) => {
_l(`[149] puppers.jscatch err: ${err}`);
await open_browser();
await wait(4000);
await connect_browser(retries_remaining - 1);
});
};
async function now_you_can_drive(wsChromeEndpointUrl) {
const browser = await puppeteer.connect({
browserWSEndpoint: wsChromeEndpointUrl,
defaultViewport: null, // <= set this to have viewport emulation off
devtools: true,
});
const new_page = await browser.newPage();
await new_page.goto(URL_HERE, {
waitUntil: "networkidle0",
});
await wait(4000);
await new_page.click('#pausar');
// id is supposed to be unique, ...lordy
}
Recommend starting with long timeouts and reduce moderately testing a run immediately after reducing. page.evaluate can be notoriously complex to deal with as a debug setup is a bit of a learning curve beyond these basics. eval blocks are super nasty cause you can't console.log easily it is possibly the worst place to learn HTML or JS, a technical challenge even for programmers. Also, I've no doubt professionals can notice you even with significant humanizer work on the interactions (jazz hands). Time flies, even knowing the code it's a lift without dev tools going which you can, but I've not set that up yet for puppeteer. Use a separate browser and treat the process like work, don't forget.
https://github.com/puppeteer/puppeteer
Version needs to approximately match if you aren't running the auto updating 'head'
You can grab the already open tab using a generator function or some other async loop I think this may already be way ahead.
Edit++ I don't use it this on youtube or facebook, not sure what general policy is there and I imagine our corpo overlords might ban your IP for botting/scraping if not careful. This script is incredibly fresh (aka working right now) Sept 2022. I don't have this kind of code in my git cause I use it for web dev mostly (which is not what this is). Also lowerCamelCase is professional and underscores_not_so_much, but I don't use QWERTY anymore and _ is better on a hold to shift kb. FYI in case you are trying to learn conventions they are lowerCamelCase almost unilaterally now. Also I don't have the rep to respond to your new question below, but Jinja doesn't run in the browser so you can't use it to make async changes in anyway. I would suggest jQuery as the goto for some basic toggling callbacks.