问题描述
我正在使用CefSharp构建Web浏览器,并且在单击按钮等方面集成了一些JavaScript。
我试图通过类名获取元素,然后将其文本保存到剪贴板。
它可以成功找到元素和文本,但是无论如何我都无法将其设置到剪贴板。
这是代码
private void button3_Click(object sender,EventArgs e)
{
CurBrowser.GetMainFrame().ExecuteJavaScriptAsync("var elems1 = document.getElementsByClassName('question-text')");
CurBrowser.GetMainFrame().ExecuteJavaScriptAsync("textt = elems1[elems1.length - 1].innerText");
CurBrowser.GetMainFrame().ExecuteJavaScriptAsync("navigator.clipboard.writeText(textt);");
}
它说该文档没有重点关注,但我尝试使用
进行重点关注textt.focus();
或
elems1.focus():
它返回“ textt.focus();不是函数”或“ elems1.focus():不是函数”
解决方法
尝试运行所有内容
CurBrowser.GetMainFrame().ExecuteJavaScriptAsync("const element = document.createElement('textarea'); element.value = document.getElementsByClassName('question-text')[0].innerText; document.body.appendChild(element); element.select(); document.execCommand('copy'); document.body.removeChild(element);");
如果仍然没有说出焦点,请尝试在其中添加焦点。