【问题标题】:Puppeteer: Can't click on input[type=radio]Puppeteer:无法点击输入[type=radio]
【发布时间】:2018-04-25 17:30:09
【问题描述】:

我正在尝试使用 Puppeteer 访问可点击的输入收音机,以推进我的应用程序调查。下面是选择器和 xpath 标签。

选择器:

#survey-engine > span > div > div > div > div > div.col-xs-12.col-xl-offset-2.col-xl-8.col-lg-offset-1.col-lg-10 > div > div:nth-child(1) > input[type="radio"]

XPath:

//*[@id="survey-engine"]/span/div/div/div/div/div[2]/div/div[1]/input

我无法让 xpath 或选择器正常工作。我也尝试过使用下面但没有运气。使用选择器查看错误:

>UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: No node found for selector: #survey-engine > span > div > div > div > div > div.col-xs-12.col-xl-offset-2.col-xl-8.col-lg-offset-1.col-lg-10 > div > div:nth-child(1) > input[type="radio"]
    at Console.assert (console.js:194:23)
    at Frame.click (/Users/erickloos/Desktop/phs-platform/node_modules/puppeteer/lib/FrameManager.js:597:13)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:48518) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:48518) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(await page.$x("//input[contains(text(), 'My Input's Text')]"))[0];

有人有什么想法吗?

【问题讨论】:

    标签: javascript jquery html css puppeteer


    【解决方案1】:
    await page.evaluate(() => {
           var test = document.querySelector('#survey-engine > span > div > div > div > div > div.col-xs-12.col-xl-offset-2.col-xl-8.col-lg-offset-1.col-lg-10 > div > div:nth-child(1) > input[type="radio"]')
            test.click();
        })
    

    【讨论】:

      【解决方案2】:

      你能不能简单地尝试一下(假设 jquery 在页面上)?

      var input = await page.evaluate(() => {
        return $('#survey-engine > span > div > ... > div:nth-child(1) > input[type="radio"]';
      });
      

      注意为了简洁起见,我从您的问题中删除了一些选择器...

      【讨论】:

      • 我会收到这个错误。 UnhandledPromiseRejectionWarning:错误:评估失败:DOMException:无法在“文档”上执行“querySelector”:“[object Object]”不是有效的选择器。当使用 document.querySelector 而不是 $ 我会得到这个错误: UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: No node found for selector: undefined
      • 如果您在 chrome 控制台中键入该选择器会发生什么?
      • 我会得到这个:init [input, prevObject: init(1), context: document, selector: "#survey-engine > span > div > div > div > div > di…10 > div > div:nth-child(1) > 输入[type="radio"]"]
      • 当我使用控制台中的实际输入的第一个索引时,我在运行我的脚本时遇到了这个错误。 UnhandledPromiseRejectionWarning:错误:评估失败:DOMException:无法在“文档”上执行“querySelector”:“#”不是有效的选择器。
      • 感谢您的帮助。能够让它工作 await page.evaluate(() => { var test = document.querySelector('#survey-engine > span > div > div > div > div > div.col-xs-12.col- xl-offset-2.col-xl-8.col-lg-offset-1.col-lg-10 > div > div:nth-child(1) > input[type="radio"]') test.click (); })
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-02
      • 2021-10-10
      • 2014-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-02
      相关资源
      最近更新 更多