【发布时间】:2020-09-14 20:42:29
【问题描述】:
^^更新^^
愿意花钱请人帮我解决这个问题,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 选择
**尝试按内部文本选择
**试图在浏览器中控制台记录正文(控制台日志记录无法在检查元素上验证)//nothing happens
**尝试使用以下方法创建带有正文的警报:
_evaluate(() => alert(document)) // nothing happens
**尝试创建警报以测试是否可以通过以下方式注入 javascript:
_evaluate(() => alert('works')) // nothing happens
**也试过这个:How to select elements within an iframe element in Puppeteer// doesn't work
这是我目前构建的代码
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');
}
【问题讨论】:
-
有效的是
'[id="isc_LinkItem_1$20j"]'。 -
你确定
await page.waitFor(time)就够了吗?你试过page.waitForSelector()吗? -
我没想到,我会试着坚持下去。
-
抱歉,如果它在 iframe 中,则应该是
frame.waitForSelector()。但是没有实时页面真的很难说这个问题( -
您在代码中的方式似乎正式有效,但没有实时页面,很难理解发生了什么。
标签: node.js google-cloud-functions puppeteer