【问题标题】:how to build linkedin bot using puppeteer如何使用 puppeteer 构建 linkedin bot
【发布时间】:2023-02-08 19:32:53
【问题描述】:

我想构建一个 LinkedIn 自动请求发送器。

任务要做?

  1. 打开 linkedin.com
  2. 使用登录详细信息登录 LinkedIn
  3. 使用关键字搜索人员
  4. 发送带有注释的连接请求。 我不能
    const select = require('puppeteer-select');
    const FORM = {
        USERNAME_SELECTOR: '#username',
        PASSWORD_SELECTOR: '#password',
        BUTTON_SELECTOR: '.btn__primary--large.from__button--floating'
    };
    const CREDENTIALS = {
        USERNAME: 'Username',
        PASSWORD: 'password'
    };
    const SEARCH = {
        SEARCH_SELECTOR: '#global-nav-search',
        KEYWORD: '',
        CONNECT: '#ember52'
    };
    
    const escapeXpathString = str => {
      const splitedQuotes = str.replace(/'/g, `', "'", '`);
      return `concat('${splitedQuotes}', '')`;
    };
    
    const clickByText = async (page, text) => {
      const escapedText = escapeXpathString(text);
      const linkHandlers = await page.$x(`//a[contains(text(), ${escapedText})]`);
    
      if (linkHandlers.length > 0) {
        await linkHandlers[0].click();
      } else {
        throw new Error(`Link not found: ${text}`);
      }
    };
    
    (async () => {
      const browser = await puppeteer.launch(); 
      const page = await browser.newPage();
      await page.goto('https://www.linkedin.com/login?trk=guest_homepage-basic_nav-header-signin', {waitUntil: 'networkidle0'});
      await page.click(FORM.USERNAME_SELECTOR);
      await page.keyboard.type(CREDENTIALS.USERNAME);
      await page.click(FORM.PASSWORD_SELECTOR);
      await page.keyboard.type(CREDENTIALS.PASSWORD);
      await page.click(FORM.BUTTON_SELECTOR);
      await page.waitForNavigation();
      await page.click(SEARCH.SEARCH_SELECTOR);
      await page.focus(SEARCH.SEARCH_SELECTOR);
      await page.keyboard.type(SEARCH.KEYWORD);
      await page.keyboard.press('Enter');
      await page.waitForNavigation();
      clickByText(page,`people`);
      await page.waitForNavigation();
      await page.screenshot({path: 'verify16.png', fullPage: true});
    
      console.log("Current page:", page.url());
     //from here
      const invitation = await select(page).getSend('span:contains(Send)');
    await invitation.click();
    //getting error
      await browser.close();
    })();```
    
    

    我无法点击连接按钮 -> 我还必须添加注释并为所有其他连接做。

【问题讨论】:

    标签: puppeteer


    【解决方案1】:
    const puppeteer = require('puppeteer');
    const select = require('puppeteer-select');
    const FORM = {
        USERNAME_SELECTOR: '#username',
        PASSWORD_SELECTOR: '#password',
        BUTTON_SELECTOR: '.btn__primary--large.from__button--floating'
    };
    const CREDENTIALS = {
        USERNAME: 'user',
        PASSWORD: 'password'
    };
    const SEARCH = {
        SEARCH_SELECTOR: '#global-nav-search',
        KEYWORD: 'keyword',
        CONNECT: '#ember52'
    };
    
    const escapeXpathString = str => {
      const splitedQuotes = str.replace(/'/g, `', "'", '`);
      return `concat('${splitedQuotes}', '')`;
    };
    
    const clickByText = async (page, text) => {
      const escapedText = escapeXpathString(text);
      const linkHandlers = await page.$x(`//a[contains(text(), ${escapedText})]`);
      
      if (linkHandlers.length > 0) {
        await linkHandlers[0].click();
      } else {
        throw new Error(`Link not found: ${text}`);
      }
    };
    
    (async () => {
      const browser = await puppeteer.launch(); 
      const page = await browser.newPage();
      await page.goto('https://www.linkedin.com/login?trk=guest_homepage-basic_nav-header-signin', {waitUntil: 'networkidle0'});
      await page.click(FORM.USERNAME_SELECTOR);
      await page.keyboard.type(CREDENTIALS.USERNAME);
      await page.click(FORM.PASSWORD_SELECTOR);
      await page.keyboard.type(CREDENTIALS.PASSWORD);
      await page.click(FORM.BUTTON_SELECTOR);
      await page.waitForNavigation();
      await page.click(SEARCH.SEARCH_SELECTOR);
      await page.focus(SEARCH.SEARCH_SELECTOR);
      await page.keyboard.type(SEARCH.KEYWORD);
      await page.keyboard.press('Enter');
      await page.waitForNavigation();
      clickByText(page,`people`);
      await page.waitForNavigation();
      await page.screenshot({path: `verifytest.png`, fullPage: true});
      const [button] = await page.$x("//button[contains(., 'Connect')]");
    if (button) {
        await button.click();
    }
    const [buttonNote] = await page.$x("//button[contains(., 'Add a note')]");
    if (buttonNote) {
        await buttonNote.click();
    }
    await page.keyboard.type('Pardon! buddy i am just testing my bot ~ Manvendra Yadav');
    const [buttonSendNote] = await page.$x("//button[contains(., 'Send')]");
    if (buttonSendNote) {
        await buttonSendNote.click();
    }
    let elements = await page.$$('#main > div > div > div:nth-child(2) > ul > li');
    // loop trough items
    for (let i = 0; i < elements.length; i++) {
          const [button] = await elements[i].$x("//button[contains(., 'Connect')]");
    if (button) {
        await button.click();
    }
    const [buttonNote] = await page.$x("//button[contains(., 'Add a note')]");
    if (buttonNote) {
        await buttonNote.click();
    }
    await page.keyboard.type('Pardon! buddy i am just testing my bot ~ Manvendra Yadav');
    await page.screenshot({path: `verify${i}.png`, fullPage: true});
    const [buttonSendNote] = await page.$x("//button[contains(., 'Send')]");
    if (buttonSendNote) {
        await buttonSendNote.click();
    }
    }
      await browser.close();
    })();
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加更多详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写出好的答案的信息in the help center
    猜你喜欢
    • 2021-05-28
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    相关资源
    最近更新 更多