Get all text from WebBrowser control - c#

I have a WebBrowser control and I will like to get all the text from that control. In other words I will like to get the same text that if I where to grab my mouse select everything from the browser and paste it in notepad. I know there are several techniques to get the text such as WebBrowser.DocumentText or innerhtml but none of those techniques gives the same text that I would get if I where to copy everything from the browser to the clipboard.

This should work:
webBrowser1.Document.ExecCommand("SelectAll", false, null);
webBrowser1.Document.ExecCommand("Copy", false, null);

Related

How to avoid HTMLElement.InnerHtml property to change relative path url of HTML elements

In winforms, I have something similar to a HTML editor where a textbox control is used to write Html code and a browser control to display a preview.
I am trying to set an InnerHtml property of a HTMLElement with something like this:
htmlElement.InnerHtml = txtCode.Text;
The problem is when assigning a string like:
"<a href='/foo/bar.aspx'>Click Here</a>"
htmlElement.InnerHtml returns:
"Click Here"
The HTML code of the InnerHtml property is saved in a file and the file is used to render content in a website which renders and invalid link.
Is there any way to avoid this behavior of the InnerHtml property, without saving the text directly from the textbox?
My only idea is to delete the text node child of the <a> element, and then append a newly-created text node with your text. This might work around whatever process is interfering with your assignment of InnerHtml.
As a workaround you can try to put script block in your txtCode:
<script>document.write("<a href='/foo/bar.aspx'>Click Here</a>")</script>

TextArea in WebBrowser

I have a WebBrowser object that I use to navigate to a forum site. Now, I am filling a form that has a textarea I need to interact with.
When I finished writing the topic and submit the value of the textarea, the value didn't set to what I wanted.
WebBrowser browser = new WebBrowser();
browser.Navigate("www.example.com");
browser.Document.All["textarea"].InnerText = "MyText";
browser.Document.All["SubmitButton"].InvokeMember("Click");
Now that didn't work, and one more important thing.. When I use the HTML visualizer to see the page while debugging, The area of the textarea in the visualizer is not displayed it says "This program cannot display the webpage" but the rest of the page is displayed normally.
I heard something about textarea that it is an HTML5 element and because the WebBrowser is an activeX that use the Internet Explorer so maybe I have a version of Internet Explorer that not support textarea elements? I have IE 10.
Can someone please tell how to set the value of a textarea properly.

Watin Visible of Text

I'm Using Watin tool in C# to find a text is available in webpage/URL. Using the code:
bool flag = browser.containsText("Some Text");
But returns true, but the text("Some Text") is hidden in page. I need to get only visible text of a URL. i Dont Have the ID/Name of the Element...
Find the control that is hidden and check to see if it is visible and contains the text.
Example if it were a Div and using NUnit:
Assert.IsTrue(myBrowser.Div.Style.GetAttributeValue("visibility") == "hidden" && myBrowser.Div("myPossiblyHiddenDiv").Text.Contains("the text"));
Lots of ways to check for the text; I usually try to go as granular as possible in case there are other controls on the page that contain the text in question.

Showing hyperlinks as plain text in a Richtextbox control

Am using RichTextBox in a C#/Winforms application.
Am showing some text in this control which has got some UNC file paths embedded into it:
for example:
filePath= "\\serverName\DirName\File"
Richtextbox shows this file path as a clickable hyperlink in the UI.
filePath= "\\serverName\DirName\File.doc"
I want to show it as plain text instead.How do i achieve this?
Thanks.
There's a property you can change named DetectUrls. It is set to true by default, set it to false to get rid of the clicking behaviour e.g.
richTextBoxName.DetectUrls = false;
Or you can simply set it to false using the properties editor in Visual Studio.
http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.detecturls.aspx

Printing content of WebBrowser Control in C#

I want to print data present in Webbrowser Control. My data is loaded from datatable which contains HTML formatted text. When I tried to print it with RichTextBox it was not able to recognize HTML controls and was not able to render.
But when I tried to print using webbrowser control, noting gets printed. How to achieve this.
webBrowser1.DocumentText = dt.Rows[0].Field<string>("WAIVER_TERMS");
webBrowser1.Print()
Did you try IWebBrowser2.ExecWB(OLECMDID_PRINT)? It should work, I've used succesully many time.

Categories

Resources