所以,正如标题所说,我想从web浏览器内网页上的元素中获取文本,并将其保存在.txt文件的新行中,但首先将其复制到剪贴板。
它可以工作,但它总是卡在同一个元素上(当元素中的文本发生变化时,剪贴板保存不会改变,所以它一直粘贴相同的文本
这是代码:
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);");
Clipboard.ContainsText(TextDataFormat.Text);
string questionfromclipboard = Clipboard.GetText(TextDataFormat.Text);
MessageBox.Show(questionfromclipboard.ToString());
File.AppendAllText(@"C:UsersRenoPCDesktopNew Questions.txt", questionfromclipboard + Environment.NewLine);
这里,试试这个:
CurBrowser.GetMainFrame().ExecuteJavaScriptAsync("var 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);");
Thread.Sleep(500);
Clipboard.ContainsText(TextDataFormat.Text);
string questionfromclipboard = Clipboard.GetText(TextDataFormat.Text);
MessageBox.Show(questionfromclipboard.ToString());
File.AppendAllText(@"C:UsersRenoPCDesktopNew Questions.txt", questionfromclipboard + Environment.NewLine);