简体   繁体   English

如何单击没有名称或 ID 的 Span

[英]How to Click a Span Without a Name or ID

I'm trying to use Pupeteer to respond to a popup dialog box, but can't find a way to click the OK button which is just an image inside a <span>我正在尝试使用 Pupeteer 来响应弹出对话框,但找不到单击“确定”按钮的方法,它只是<span>中的图像

This is what I've tried so far:到目前为止,这是我尝试过的:

  const [button] = await frame.$x('/html/body/div/div[4]/div[2]/div/span[1]');
  
  if (button) {
       await button.click()
  }

But the button is not found.但是找不到按钮。

This is the pertinent section of the html:这是 html 的相关部分:

   <div class="vtm_text vtm_exportDialog"><input type="hidden" value="save">
      ...
      <span class="vtmBtn" style="min-width: 60px; padding: 3px;">OK</span>
      <span class="vtmBtn" style="min-width: 60px; padding: 3px;">Cancel</span>
    </div>

This is what the page looks like when inspected in Chrome:这是在 Chrome 中检查时页面的样子:

这是在 Chrome 中检查时页面的样子

const [button] = await frame.$x("/html/body/div/div[4]/div[2]/div/span[1]");

async function pop_up() {
  if (button) {
    await button.click();
  }
}

document.getElementsByClassName("vtmBtn").addEventListener("click", (e) => {
  var img = document.createElement("img");
  img.setAttribute("src", "");
  //   in the empty string put the path of the image you want

  e.target.appendChild(img);
});

I was able to solve this by using $eval as follows:我能够通过使用 $eval 解决这个问题,如下所示:

await frame.$eval('span[class=vtmBtn]', el => el.click());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM