node.js - 为什么 Puppeteer 在页面上找不到这个链接元素?

标签 node.js google-cloud-functions puppeteer

^^更新^^
愿意花钱请人引导我完成这个问题,在 codeMentor.io 上发布的问题:https://www.codementor.io/u/dashboard/my-requests/9j42b83f0p

我一直在寻找点击该元素:

<a id="isc_LinkItem_1$20j" href="javascript:void" target="javascript" tabindex="2"
onclick="if(window.isc_LinkItem_1) return isc_LinkItem_1.$30i(event);"
$9a="$9d">Reporting</a>

在:https://stackblitz.com/edit/js-nzhhbk (我没有包含实际页面,因为它位于用户名和密码后面)

看起来很简单
-------------------------------------------------- --------------------

解决方案1:

page.click('[id=isc_LinkItem_1$20j]') //not a valid selector


解决方案2:

const linkHandlers = await frame.$x("//a[contains(text(), 'Reporting')]");

if (linkHandlers.length > 0) {
    await linkHandlers[0].click();
} else {
    throw new Error('Link not found');
} //link not found

------------------------------------------------------------ -----------------------

我已经查看了选择并单击它的每种方式,它说它不在文档中,尽管它显然是(通过检查 chrome 开发工具中的 html 并调用:page.evaluate( () => document.body.innerHTML))

**尝试查看它是否在 iframe 中
**尝试通过 id 选择
**尝试通过内部文本进行选择
**尝试在浏览器中控制台记录正文(控制台日志记录未在检查_元素上进行验证)//什么也没有发生
**尝试使用以下方法创建带有正文的警报​​:
_evaluate(() =>alert(document))//什么也没发生
**尝试创建一个警报来测试是否可以通过以下方式注入(inject) JavaScript:
_evaluate(() =>alert('works'))//什么也没有发生
**也尝试过这个:How to select elements within an iframe element in Puppeteer //不起作用

这是我迄今为止构建的代码

const page = await browser.newPage();

const login1url =
    'https://np3.nextiva.com/NextOSPortal/ncp/landing/landing-platform';
await page.goto(login1url);
await page.waitFor(1000);
await page.type('[name=loginUserName', 'itsaSecretLol');
await page.type('[name=loginPassword]', 'nopeHaha');
await page.click('[type=submit]');
await page.waitForNavigation();
const login3url = 'https://np3.nextiva.com/NextOSPortal/ncp/admin/dashboard';
await page.goto(login3url);
await page.click('[id=hdr_users]');
await page.goto('https://np3.nextiva.com/NextOSPortal/ncp/user/manageUsers');
await page.goto('https://np3.nextiva.com/NextOSPortal/ncp/user/garrettmrg');
await page.waitFor(2000);
await page.click('[id=loginAsUser]');
await page.waitFor(2000);
await page.click('[id=react-select-5--value');
await page.waitFor(1000);
await page.click('[id=react-select-5--option-0]');
await page.waitFor(20000);
const elementHandle = await page.$('iframe[id=callcenter]');
const frame = await elementHandle.contentFrame();
const linkHandlers = await frame.$x("//a[contains(text(), 'Reporting')]");

if (linkHandlers.length > 0) {
    await linkHandlers[0].click();
} else {
    throw new Error('Link not found');
}

最佳答案

由于isc_LinkItem_1$20j不是有效的选择器,也许您可​​以尝试查找元素STARTING WITH isc_LinkItem_1 ,像这样

await page.waitForSelector("[id^=isc_LinkItem_1]", {visible: true, timeout: 30000});
await page.click("[id?=isc_LinkItem_1]);

关于node.js - 为什么 Puppeteer 在页面上找不到这个链接元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62049361/

相关文章:

node.js - 使用 NodeJS 和 Restify 重定向客户端

c# - 如何从 puppeteer-sharp 中获取可读的浏览器/页面错误?

javascript - 使用 Puppeteer 定位 iFrame 中的元素

node.js - 为什么这个 firebase 函数会递归运行?

firebase - 如何使用 HTTP 调用 Firebase 可调用函数?

google-cloud-functions - 云功能不下载依赖项

javascript - JavaScript 中 &= 的含义是什么

node.js - 在电子构建器中配置 pkg 选项脚本的最佳方法是什么?

node.js - 解析 Post 表单数据 Node.js Express

node.js - 多对多关联抛出 "A is not associated to B"错误