【问题标题】:Why promise not working in golang chromedp为什么承诺不在 golang chromedp 中工作
【发布时间】:2020-04-19 13:54:19
【问题描述】:

我实际上试图在golang chromedp 中执行这段代码,但没有工作

const inlineJavascript = `
    var textarea = document.createElement('textarea');
    textarea.setAttribute('id', 'recaptcha-token-container');
    textarea.style.display = 'display:none;'
    document.body.appendChild(textarea);

    grecaptcha.execute('SITE_KEY', { action: 'login' }).then(function(token) {
        textarea.value = token;
        textarea.style.display = ''
    });
`
chromedp.EvaluateAsDevTools(inlineJavascript, &token),
chromedp.WaitVisible(`#recaptcha-token-container`),
chromedp.Value(`#recaptcha-token-container`, &token),

应用程序等待 textarea,.then 不工作并且 textarea never 正在显示。

【问题讨论】:

  • 我假设 SITE_KEY 设置正确。当您将 sn-p 粘贴到浏览器的开发工具中时,它是否有效?您可能还想处理错误并将它们显示在您可以看到的地方
  • @xarantolus SITE_KEY 已修改,是的,在 devtools 中工作正常,但返回。
  • log.Println(token) 返回map[then:map[]]

标签: go promise webdriver chromedp


【解决方案1】:

WaitVisible 等到您的元素可见。

在您的textarea.style.display = 'display:none;' 行中,CSS 样式无效并因此被忽略,这意味着这是您的元素可见的点。 该行应该是

textarea.style.display = 'none'

所以WaitVisibledocument.body.appendChild(textarea); 运行之后的那一刻返回,这意味着它之后的所有内容都不会影响token

当你更改了不正确的 CSS 时,它应该可以工作,因为该元素只有在 Promise 运行后才可见

【讨论】:

  • 感谢@xarantolus,事实上textarea 从未被隐藏并且WaitVisible 没有等待元素,问题是javascript Promise。解决方案:` (async () => { var t = await grecaptcha.execute('SITE_KEY', { action: '%v' }).then(function(token) { return token; }); textarea.value = t ; textarea.style.display = '' })() `
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 2019-04-29
  • 2022-01-15
  • 1970-01-01
  • 2018-04-09
相关资源
最近更新 更多