【发布时间】:2019-12-12 10:26:59
【问题描述】:
我正在尝试点击 Linkedin 个人资料中的“我的网络”按钮。我可以使用 'goto' 方法直接转到该网址,但我希望能够点击它。
我曾尝试使用“复制选择器”,但它似乎不起作用。
const BUTTON_SELECTOR = '#mynetwork-nav-item';
await page.click(BUTTON_SELECTOR);
我可以登录,但之后就无法登录了。
完整代码-
const puppeteer = require('puppeteer');
(async () => {
/* Initiate the Puppeteer browser */
const browser = await puppeteer.launch({ headless: false, defaultViewport: { width: 1366, height: 768 }});
const page = await browser.newPage();
await page.goto(`https://www.linkedin.com`, { waitUntil: 'networkidle0' });
let element, formElement, tabs;
element = await page.$x(`//a[contains(text(),'Sign in')]`);
await element[0].click();
await page.waitForNavigation();
element = await page.$x(`//*[@id="username"]`);
await element[0].type(`some_id@example.com`);
element = await page.$x(`(.//*[normalize-space(text()) and normalize-space(.)='Welcome Back'])[1]/following::div[3]`);
await element[0].click();
element = await page.$x(`//*[@id="password"]`);
await element[0].click();
element = await page.$x(`//*[@id="password"]`);
await element[0].type(`some_password`);
await page.keyboard.press(`Enter`)
const MY_NETWORK_BUTTON = '#mynetwork-tab-icon';
await page.waitForSelector(MY_NETWORK_BUTTON, {visible: true});
const [response] = await Promise.all([
page.waitForNavigation(), // The promise resolves after navigation has finished
page.click(MY_NETWORK_BUTTON), // Clicking the link will indirectly cause a navigation
]);
console.log('CURRENT URL IS: ', response.url());
})();
【问题讨论】:
标签: node.js puppeteer google-chrome-headless