微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

可以使用 puppeteer 自动点击子菜单吗?

如何解决可以使用 puppeteer 自动点击子菜单吗?

我正在尝试使用 puppeteer 自动点击子菜单,但这不起作用

var frames = await serv.page.frames();
let lastFrame = frames[frames.length-1];
await lastFrame.waitForNavigation();

await lastFrame.waitForSelector('.reportBuilder-view');
const [more] = await lastFrame.$x("//button[contains(.,'More Actions')]");
await more.click();

console.log(await lastFrame.content());
await serv.wait(1000);
console.log(await lastFrame.content());
console.log('wait for subscribe?')

await lastFrame.waitForSelector('a[title="Subscribe"]');
await lastFrame.click('a[title="Subscribe"]');

我可以通过行 await more.click();

模拟点击“更多操作”按钮

但是,我无法点击子菜单项。

脚本在第 await lastFrame.waitForSelector('a[title="Subscribe"]'); 行失败,无法选择正确的锚元素。

UnhandledPromiseRejectionWarning: TimeoutError: waiting for selector `a[title="Su
bscribe"]` Failed: timeout 30000ms exceeded

我是 puppeteer 的新手,我不明白为什么此代码不起作用。

也许问题是子菜单嵌入了一个绝对定位的元素?

outline: 0px; position: absolute; right: inherit; top: 52px; left: 615px;

也许它是动态添加到 DOM 中的,但为什么它已经显示添加到 DOM 30 秒内找不到它?

当我查看 lastFrame.content()输出时,我可以看到该元素存在于 DOM 中

<li class="slds-dropdown__item actionBarButto
nGroup-action-item report-action-ReportScheduleAction" id="liVM0LRxvZ-item-3" role="presentati
on"><a aria-disabled="false" href="javascript:void(0);" data-index="3" role="menuitem" tabinde
x="-1"><span class="slds-truncate" title="Subscribe">Subscribe</span></a></li>

在这里做错了吗?

我也试过等待 XPath,但这也没有用

await lastFrame.waitForXPath("//li[@class='report-action-ReportScheduleAction']/a");


const [li] = await lastFrame.$x("//li[@class='report-action-ReportScheduleAction']");
console.log('try to click the li');
await li.click();

解决方法

实际上标题是跨度的属性而不是锚点。

nvm install 4
nvm use 4

当我点击跨度时,它起作用

<li class="slds-dropdown__item actionBarButto
    nGroup-action-item report-action-ReportScheduleAction" id="liVM0LRxvZ-item-3" role="presentati
    on">
    <a aria-disabled="false" href="javascript:void(0);" data-index="3" role="menuitem" tabinde
    x="-1">
        <span class="slds-truncate" title="Subscribe">Subscribe</span>
    </a>
</li>

仍然不明白为什么 await lastFrame.waitForSelector('span[title="Subscribe"]'); await lastFrame.click('span[title="Subscribe"]'); 选择器不起作用

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。