Xetera / ghost-cursor

🖱️ Generate human-like mouse movements with puppeteer or on any 2D plane
MIT License
1.03k stars 120 forks source link

Is it possible to add work with an element directly? #3

Closed ergcode closed 3 years ago

ergcode commented 4 years ago

/src/spoof.ts

    async click(selector: any) {
      if (selector) {
        await actions.move(selector);
      }
      return page.mouse.down().then(() => page.mouse.up());
    },
    async move(selector: any) {
            const elem = (typeof selector === 'string')
                ? await page.$(selector)
                : await selector;
      if (!elem) {```

Example: 
    let cursor = createCursor(page);
    await page.goto(
        'https://infosimples.github.io/detect-headless/', {
        waitUntil: ['domcontentloaded'],
        30000,
    });

    let select = await page.$('#mouse-move-result');
    await cursor.move(select);
    select = await page.$('#plugins-prototype-result');
    await cursor.move(select);
    await cursor.click(select);


What for? There is a case when elements are enumerated, for example, a form with data.
And it’s not always possible to precisely bind to an element selector.
kekearif commented 3 years ago

Bump. I have multiple buttons with the same selector on a page. I need to iterate them and click them. Is this at all possible with ghost-cursor ?

Niek commented 3 years ago

Fixed in https://github.com/Xetera/ghost-cursor/pull/10 - support for XPath added.

BTW, if you want to use CSS selectors you can also use nth-child() pseudo selectors: https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child