Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用Puppeter单击页面上的任意位置_Node.js_Puppeteer - Fatal编程技术网

Node.js 使用Puppeter单击页面上的任意位置

Node.js 使用Puppeter单击页面上的任意位置,node.js,puppeteer,Node.js,Puppeteer,目前,我正在使用Puppeter从页面获取cookies和标题,但是它使用的是机器人预防系统,只有在点击页面时才被绕过;我不想让它保持顺序,所以它是“可检测的” 我怎么能让我的木偶师随意点击页面上的任何地方?不管它是否点击链接、按钮等 我现在有这个代码 const getCookies = async (state) => { try { state.browser = await launch_browser(state); state.conte

目前,我正在使用Puppeter从页面获取cookies和标题,但是它使用的是机器人预防系统,只有在点击页面时才被绕过;我不想让它保持顺序,所以它是“可检测的”

我怎么能让我的木偶师随意点击页面上的任何地方?不管它是否点击链接、按钮等

我现在有这个代码

const getCookies = async (state) => {
    try {
        state.browser = await launch_browser(state);
        state.context = await state.browser.createIncognitoBrowserContext();
        state.page = await state.context.newPage();
        await state.page.authenticate({
            username: proxies.username(),
            password: proxies.password(),
        });
        await state.page.setViewport(functions.get_viewport());
        state.page.on('response', response => handle_response(response, state));
        await state.page.goto('https://www.website.com', {
            waitUntil: 'networkidle0',
        });
        await state.page.waitFor('.unlockLink a', {
            timeout: 5000
        });
        await state.page.click('.unlockLink a');
        await state.page.waitFor('input[id="nondevice"]', {
            timeout: 5000
        });
        state.publicIpv4Address = await state.page.evaluate(() => {
            return sessionStorage.getItem("publicIpv4Address");
        });
        state.csrfToken = await state.page.evaluate(() => {
            return sessionStorage.getItem("csrf-token");
        });
        //I NEED TO CLICK HERE! CAN BE WHITESPACE, LINK, IMAGE
        state.browser_cookies = await state.page.cookies();
        state.browser.close();
        for (const cookie of state.browser_cookies) {
            if(cookie.name === "dtPC") {
                state.dtpc = cookie.value;
            }
            await state.jar.setCookie(
                `${cookie.name}=${cookie.value}`,
                'https://www.website.com'
            )
        }
        return state;
    } catch(error) {
        if(state.browser) {
            state.browser.close();
        }
        throw new Error(error);
    }
};

我能想到的从DOM中选择随机元素的最简单方法可能是使用,它将返回文档中所有
的数组(或选择任何其他元素,如
或任何其他元素),然后您可以轻松地从结果中随机使用
单击(),例如:

await page.evaluate(() => {
  const allDivs = document.querySelectorAll('.left-sidebar-toggle');
  const randomElement = allDivs[Math.floor(Math.random() * allDivs.length)];
  randomElement.click();
});

为什么不直接使用
wait state.page.点击('body')
(或其他文档元素,而不是
“body”
)?@errata,如果我使用
等待状态。页面。单击('body')是否每次都单击相同的X/Y轴?