简体   繁体   中英

How to get text from element and save it to a new line in .txt file using javascript?

So, as the title says, I want to get a text from an element on the web page inside my web browser and save it in a.txt file's new line, but first copy it to clipboard.

It works, but it is always stuck at the same element (clipboard save doesn't change when the text in element is changed so it keeps pasting the same text

here is the code:

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:\Users\RenoPC\Desktop\New Questions.txt", questionfromclipboard + Environment.NewLine);

Here, try this:

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:\Users\RenoPC\Desktop\New Questions.txt", questionfromclipboard + Environment.NewLine);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM