【问题标题】:Can puppeteer be used to automate clicking on submenu?puppeteer 可以用来自动点击子菜单吗?
【发布时间】:2021-05-29 14:24:58
【问题描述】:

我正在尝试使用 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 中的,但为什么 30 秒后它已经显示并添加到 DOM 中却找不到它?

当我查看来自 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();

【问题讨论】:

  • 那个菜单是动画的吗?
  • 不确定。实际上,我可能应该点击跨度而不是锚点......

标签: puppeteer ui-automation browser-automation


【解决方案1】:

其实title是span的属性,而不是anchor。

<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"]');

仍然没有弄清楚为什么li 选择器不起作用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多