-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Running Puppeteer in Headless Mode for Captcha Solving #49
Comments
Hi, this system works in simple cloudflare encounters but not in a situation like this. It might be because the captcha is clicked with javascript and the targetFilter function is not enough. Since the purpose of the library is not only cloudflare (e.g. google login), we use it this way. You can use wsl and docker in Windows environment to start the browser incognito. 2024-05-08.22-17-40.mp4 |
I have found the reason why headless mode is not working, and it is because the user-agent within the iframes does not change. This is because the program skips the iframes. When I changed the user agents through remote debugging using DevTools, the headless mode started working. The only problem is changing the user agents within iframes without using Puppeteer |
https://hmaker.github.io/selenium-detector/ |
const {
RequestInterceptionManager,
} = require("puppeteer-intercept-and-modify-requests");
const puppeteer = require("puppeteer-core");
function targetFilter(target) {
const session = target._session();
if (session) {
session.send = new Proxy(session.send, {
apply(target, thisArg, args) {
if ("Runtime.enable" === args[0]) {
return Promise.resolve();
} else {
const result = Reflect.apply(target, thisArg, args);
return result;
}
},
});
}
return true;
}
const script = `<script>
Element.prototype._addEventListener = Element.prototype.addEventListener;
Element.prototype.addEventListener = function () {
let args = [...arguments]
let temp = args[1];
args[1] = function () {
let args2 = [...arguments];
args2[0] = Object.assign({}, args2[0])
args2[0].isTrusted = true;
return temp(...args2);
}
return this._addEventListener(...args);
}
const targetSelector = 'input[type=checkbox]';
const observer = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList') {
const addedNodes = Array.from(mutation.addedNodes);
for (const addedNode of addedNodes) {
if (addedNode.nodeType === addedNode.ELEMENT_NODE) {
const node = addedNode?.querySelector(targetSelector);
if (node) {
setTimeout(()=>{
node.parentElement.click();
},1000);
}
}
}
}
}
});
const targetElement = document.documentElement;
const observerOptions = {
childList: true,
subtree: true
};
observer.observe(targetElement, observerOptions);
//document.querySelector('script').remove();
</script>`;
async function main() {
const browser = await puppeteer.launch({
ignoreDefaultArgs: ["--enable-automation"],
args: ["--disable-blink-features=AutomationControlled"],
defaultViewport: null,
executablePath:
"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
headless: "new",
debuggingPort: 9222,
targetFilter,
});
let page = (await browser.pages())[0];
await page.setViewport({
width: 1920,
height: 1080,
});
await page.setUserAgent(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
);
const client = await page.target().createCDPSession();
const interceptManager = new RequestInterceptionManager(client);
await interceptManager.intercept({
urlPattern: `https://challenges.cloudflare.com/*`,
resourceType: "Document",
modifyResponse({ body }) {
return {
body: body.replaceAll("<head>", "<head>" + script),
};
},
});
console.log("Connected to browser");
await page.goto("https://nopecha.com/demo/cloudflare", {
waitUntil: "domcontentloaded",
});
await page.waitForNavigation();
await page.waitForNavigation();
await page.waitForNavigation();
await page.screenshot({ path: "example.png" });
console.log("Closing browser");
await browser.close();
}
main(); I wrote the above code to bypass the |
When I activate the
headless mode
, Puppeteer can't solve thecapctra
.Is there a way to run it in headless mode?
Additionally, the following solution is more accurate. Since we disable Puppeteer's access to Cloudflare, there won't be iframe access, so it would be more appropriate to manipulate the response and communicate with the iframe for a more accurate solution.
The text was updated successfully, but these errors were encountered: