简体   繁体   English

Javascript/HTML/Puppeteer - 如何访问属性数据绑定中的值(单击按钮)?

[英]Javascript/HTML/Puppeteer - How to access the values from the attribute data-bind (to click a button)?

I'm currently using Puppeteer to click a button on a page.我目前正在使用 Puppeteer 单击页面上的按钮。 The problem I am facing is that the selected element to click, loads the button for a certain period of time which I suppose is arbitrary to my internet connection or perhaps even additional parameters (so even if I use the method waitForSelector() , the button won't be clickable although the element is present, because it is still loading).我面临的问题是要单击的选定元素会在一段时间内加载按钮,我认为这对我的互联网连接或什至可能是附加参数是任意的(因此即使我使用方法waitForSelector() ,按钮尽管该元素存在,但将无法点击,因为它仍在加载)。

I could add a function waitFunc(seconds) but the script will break if the button load time is superior to the waitFunc(5) time (so it's not always going to work, unless the wait time is arbitrarily long enough but then it defies the purpose of having the script).我可以添加一个function waitFunc(seconds)但如果按钮加载时间优于waitFunc(5)时间,脚本就会中断(所以它并不总是会起作用,除非等待时间足够长,但它无视拥有脚本的目的)。 My requirement would be to click the button as soon as it has loaded.我的要求是在加载后立即单击该按钮。 How can we do this?我们应该怎么做?

In the button's tag below, I see that there is loadingButton in data-bind .在下面按钮的标签中,我看到data-bind中有loadingButton I anticipate that it could return a boolean.我预计它可以返回一个布尔值。 Nevertheless, how can I access this data with Puppeteer and create a function to click this button once loaded?然而,如何使用 Puppeteer 访问此数据并创建一个函数以在加载后单击此按钮?

<
  button
    type="submit"
    data-bind="
      buttonText: 'ClickMe',
      click: clickMe,
      loadingButton: loading,
      enable: canClickMe"
    class="btn-class"
>
  Click Me!
</button>

I've done some research on Knockout, but I don't understand it enough to manipulate this tool.我对 Knockout 做了一些研究,但我对它的理解不足以操纵这个工具。 I imagine that once that I manage to get the value of loadingButton , I can use a while loop to click the button once that its value changed.我想,一旦我设法获得了loadingButton的值,我可以使用 while 循环在其值发生变化后单击该按钮。

I've tried const attr = await page.$eval(selectors.buyNow, el => el.map(x => x.getAttribute("data-bind"))) , but the returned value is the string "buttonText: 'ClickMe', click: clickMe, loadingButton: loading, enable: canClickMe" (literally what is in the HTML button tag attribute data-bind ).我试过const attr = await page.$eval(selectors.buyNow, el => el.map(x => x.getAttribute("data-bind"))) ,但返回的值是字符串"buttonText: 'ClickMe', click: clickMe, loadingButton: loading, enable: canClickMe" (字面意思是 HTML 按钮标签属性data-bind )。

So you want to click that button?所以你想点击那个按钮? How about:怎么样:

let button = await page.waitForSelector(`button[data-bind*="loadingButton"]`)
await button.click()

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

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