maybe not the most powerful solution out there but if you only need to do this on this specific tag you could set onclick
's attribute with JavaScript within page.evalauate
like this:
await page.evalauate(() => {
document
.querySelector(".btn.btn-info")
.setAttribute(
"onclick",
document
.querySelector(".btn.btn-info")
.onclick.toString()
.split("\n")[1]
.replace(",'_blank'", "")
);
});
await page.click(".btn.btn-info");
what's going on here?
- we run plain JavaScript within the page's scope with
page.evaluate
- we select the tag with
document.querySelector
- and set its
onclick
attribute (not its property!)
- getting the node value of
onclick
as string:
mojarra.jsfcljs(document.getElementById('j_idt58'),{'j_idt58:j_idt201:0:j_idt203':'j_idt58:j_idt201:0:j_idt203'},'_blank');return false
- using the 2nd line of the function (as we don't need the 1st
'function onclick(event) {'
line when we reassign it as the attribute)
- and replacing
,'_blank'
parameter from the original function (string).
- the result will be:
mojarra.jsfcljs(document.getElementById('j_idt58'),{'j_idt58:j_idt201:0:j_idt203':'j_idt58:j_idt201:0:j_idt203'});return false
- finally clicking the button with
page.click
executes the new function
alternatively, you can use attributes.onclick.nodeValue
if you are not comfortable with toString().split("\n")[1]
above:
document.querySelector(".btn.btn-info").attributes.onclick.nodeValue.replace(",'_blank'", "")