Node.js 为什么木偶演员在页面上找不到这个链接元素?
^^更新^Node.js 为什么木偶演员在页面上找不到这个链接元素?,node.js,google-cloud-functions,puppeteer,Node.js,Google Cloud Functions,Puppeteer,^^更新^ 愿意花钱请人帮我解决这个问题,发布在codeMentor.io上: 我一直在寻找点击元素: <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> 解决方案2:
愿意花钱请人帮我解决这个问题,发布在codeMentor.io上: 我一直在寻找点击元素:
<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>
解决方案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选择
**试图通过内部文本进行选择
**试图在浏览器中对主体进行控制台日志记录(控制台日志记录未在inspect _元素上运行)//未发生任何情况
**尝试使用以下命令创建带有正文文本的警报:
evaluate(()=>alert(document))//什么也没有发生
**试图创建一个警报来测试javascript是否可以通过以下方式注入:
\uevaluate(()=>alert('works')//什么都没有发生
**还尝试了以下操作://不起作用
以下是我迄今为止构建的代码
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不是有效的选择器,也许您可以尝试查找元素isc_LinkItem_1,如下所示
await page.waitForSelector("[id^=isc_LinkItem_1]", {visible: true, timeout: 30000});
await page.click("[id?=isc_LinkItem_1]);
?关于您的解决方案1:
await page.click('a[id=isc_LinkItem_1\\$20j]');
或者尝试:
await page.click('#isc_LinkItem_1\\$20j]');
我有一个轻微的印象,您必须在括号之前提供您试图选择的元素类型,在本例中是元素
在第二种解决方案中,#字符意味着我们正在通过元素的id选择元素。结果表明,上一次单击触发了一个新选项卡。木偶演员没有移动到新标签页,所有以前的代码都在旧标签页上执行。要解决所有问题,我们只需找到新选项卡,选择它并执行代码,下面是我们编写的为选项卡选择的函数:
async function getTab(regex, browser, targets) {
let pages = await browser.pages();
if (targets) pages = await browser.targets();
let newPage;
for (let i = 0; i < pages.length; i++) {
const url = await pages[i].url();
console.log(url);
if (url.search(regex) !== -1) {
newPage = pages[i];
console.log('***');
console.log(url);
console.log('***');
break;
}
}
console.log('finished');
return newPage;
}
异步函数getTab(正则表达式、浏览器、目标){
let pages=等待browser.pages();
if(targets)pages=wait browser.targets();
让我们来看看newPage;
for(设i=0;i
有效的是'[id=“isc\u LinkItem\u 1$20j”]
。您确定等待页面。等待(时间)
就足够了吗?您是否尝试过page.waitForSelector()
?我没有想到,我会尝试一下,请稍候。对不起,如果它位于iframe中,则应该是frame.waitForSelector()
。但如果没有实时页面,就很难说出问题所在(您在代码中的方式似乎正式有效,但如果没有live页面,则很难理解发生了什么。我认为,无论出于何种原因,它不在页面上,而是在其他地方,这怎么可能?我编辑了我的答案,在选择器中的$之前提供了两个反斜杠,请尝试使用该选项。此外,正如我所看到的,链接上的iFrame您可以使用。)“已通过的元素在打开后立即关闭,这意味着它们已清空无法工作,如果这样可以更方便的话,我们可以进行屏幕共享,我可以向您显示live元素树,NDA不允许我共享凭据加载页面后,当您尝试通过浏览器控制台选择元素时会发生什么情况?如果您尝试单击元素访问页面后,可能尚未加载DOM内容,这可能是您找不到该元素的原因。请尝试设置一个wait页面。waifFor(5000)
在执行clickBTW之前,您不需要wait
页面或目标.url()
。此外,您可以尝试使用const newPage=pages.find(page=>regex.test(page.url());
,而不是循环,这两种方法都可以使代码更加简洁!
async function getTab(regex, browser, targets) {
let pages = await browser.pages();
if (targets) pages = await browser.targets();
let newPage;
for (let i = 0; i < pages.length; i++) {
const url = await pages[i].url();
console.log(url);
if (url.search(regex) !== -1) {
newPage = pages[i];
console.log('***');
console.log(url);
console.log('***');
break;
}
}
console.log('finished');
return newPage;
}