How do I click a button that has no ID using (Apify's) Puppeteer?

荒凉一梦 提交于 2021-02-11 12:25:45

问题


I am using Apify's puppeteer to login to this website. I did research similar questions but to no avail.

I am having trouble finding the clickable id/element for the main Login button seen on the linked login page. Currently, my code reads like this:

const Apify = require('apify');

Apify.main(async () => {
const input = await Apify.getValue('INPUT');

const browser = await Apify.launchPuppeteer();
const page = await browser.newPage();
await page.goto('https://www.sunpass.com/vector/account/home/accountLogin.do');

// Login
await page.type('#tt_username1', input.username);
await page.type('#tt_loginPassword1', input.password);
await page.waitFor(2000);
await page.click('#entryform input');
await page.waitForNavigation();

// Get cookies
const cookies = await page.cookies();

// Use cookies in other tab or browser
const page2 = await browser.newPage();
await page2.setCookie(...cookies);
await page2.goto('https://www.sunpass.com/vector/account/transactions/webtransactionSearch.do'); // Opens page as logged user

await browser.close();

console.log('Done.');

With the id entryform I receive the following error: Node is either not visible or not an HTMLElement

With the id loginP I receive the following error: No node found for selector

I used XPath to locate these, it offered no other ids of use. Any help would be greatly appreciated on how to find a clickable element for this login button, or any other method.


回答1:


You have to try another selector. I tried button[name="btnLogin"] and it worked.

tested code:

const Apify = require('apify');

Apify.main(async () => {
    const input = await Apify.getValue('INPUT');

    const browser = await Apify.launchPuppeteer();
    const page = await browser.newPage();
    await page.goto('https://www.sunpass.com/vector/account/home/accountLogin.do');

    // Login
    await page.type('#tt_username1', input.username);
    await page.type('#tt_loginPassword1', input.password);
    await page.waitFor(2000);
    await page.click('button[name="btnLogin"]');
    await page.waitForNavigation();

    // Get cookies
    const cookies = await page.cookies();

    // Use cookies in other tab or browser
    const page2 = await browser.newPage();
    await page2.setCookie(...cookies);
    await page2.goto('https://www.sunpass.com/vector/account/transactions/webtransactionSearch.do'); // Opens page as logged user

    await browser.close();

    console.log('Done.');
});



回答2:


During my test of the login form on a desktop the "LOGIN" button could be found with this selector:

button[name=btnLogin].btn-large


来源:https://stackoverflow.com/questions/53165242/how-do-i-click-a-button-that-has-no-id-using-apifys-puppeteer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!