我实际上试图在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
。
最佳答案
WaitVisible
等待直到您的元素可见。
在textarea.style.display = 'display:none;'
行中,CSS样式无效,因此被忽略,这意味着这是您的元素可见的地方。
该行应该是
textarea.style.display = 'none'
因此,
WaitVisible
在document.body.appendChild(textarea);
运行之后立即返回,这意味着之后的所有内容都不会影响token
。当您更改错误的CSS时,它应该可以正常工作,因为该元素仅在promise运行后才可见
关于go - 为什么 promise 不能在Golang chromedp中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59537265/