Find text in webbrowser control - c#

I have a web browser control, already navigated to a page. Now I want to search for a specific text in the page, and scroll to the first occurrence.
Being able to scroll to other occurrences is a bonus.

You can try this code:
webBrowser1.Select();
SendKeys.Send("^f");

I don't know if it works in a WebBroswer. We make the broswer(IE/FF/etc) window scroll to some text with the following code:
//source code of http://www.sample.com/sample.html
<html>
...
<span name="aim">KeyWord</span>
...
</html>
If I want the window to scroll to the "KeyWord", simply visit http://www.sample.com/sample.html#aim
Using string document = myWebBrowser.DocumentText to get the source code of the page, and search the text in the string, get its node name, and navigate it using #.

See this if it helps:
string PageSource = null;
PageSource = WebBrowser1.Document.Body.InnerHtml();
if (Strings.InStr(PageSource, stringtoFind) > 0) {
...insert an Anchor tag here and then use
WebBrowser1.Navigate to go to the the new URL with the #Anchor tag
} else {
...whatever else
}

One way...
Use the Ctrl + F Key to invoke Find, native to the WebBrowser Control?

Related

How to click on all elements with the same class/parametr WatiN

I using WatiN dll, i have a table and in each td i have link with <a> href =javascript:void(0) </a> ,i my case i have same href in all of them(they dont have class),i need to click on them all (they open new tr with data) ,and then save the html page,my problem to click on all of them , i can click on the first one like this
Frame frameBODY = browser.Frame(Find.ByName("BODY"));
frameBODY.Link(Find.By("href", "javascript:void(0)")).Click();
But i need to click on all links that have "href", "javascript:void(0)",i think i need you use ListCollection but i new in WatiN and still cant find the way to do this.
Any ideas how click on all links with "href", "javascript:void(0)".
You can get all the links inside of frame body as below.
Frame frameBODY = browser.Frame(Find.ByName("BODY"));
LinkCollection links = frameBODY.Link;
foreach(Link link in links)
{
if(link.GetAttributeValue("href").Contains("javascript:void(0)"))
{
link.Click();
// TODO - Add logic here for saving the file.
}
}

How to check if specified tag is inside in rad editor content

I am using Telerik Rad editor. using vs 2010 with c#.
I bind the content to editor during page load.
On button click event ,i need to check if th particular tag (for ex: ) is inside the content in HTML mode.
I am using the following code.
if (Radeditor1.Content.Contains("<title/>"))
{
}
Tag has value like welcome.
so Content .contains take it like as a string and return false.
How to check it.
Thanks and Regards,
Pooja
I could be seeing it wrong but if you just want to find the start of the tag you could use:
if (Radeditor1.Content.Contains("<title"))
{
}
If you want to retrieve the contents of the tag you could use Regex like so
System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(Radeditor1.Content, #"\<title[^\<]*\>(?<content>.*?)\<\/title\>", RegexOptions.Multiline);
if (match.Success)
{
string content = match.Groups["content"].Value;
}

How to enter the text in textarea using mshtml

I am facing a problem I have to fill a TextArea programatically. I used the following code.
System.Windows.Forms.HtmlElement reviewText = myDoc.GetElementById("review-text");
reviewText.InnerText = Program.mainForm.richTextBox1.Text.Trim();
This works fine and it sets the text in TextArea control. The problem that I am facing is that this text appears light gary. When the user clicks over this text it disappears. It happens only on first click.
So I tried the following code to first click this box and then set the text.
reviewText.InvokeMember("click");
And then tried to set the text in the TextArea but got the same behaviour.
I dug into the source of the page and found some script associated with this TextArea.
Source of TextArea
<textarea onkeyup="javascript:yelp.ui.widget.countChars(this.form.comment,5000);" onkeydown="javascript:yelp.ui.widget.countChars(this.form.comment,5000);" name="comment" id="review-text" class="form400" rows="8" cols="40" style="height: 86px;"></textarea>
Script associated with TextArea
<script type="text/javascript">
var bizName = "Geary Club";
if ($('review-text'))
{
new yelp.ui.widget.DefaultValueTextField($('review-text'), 'Please write here');
}
Will somebody suggest to me how to insert the text into this TextArea control?
Is this an ASP.NET web app? If it is, the System.Windows.Forms.HtmlElement is a problem. What I think is going on is that you need to somehow disable the new yelp.ui.widget.DefaultValueTextField JavaScript code when you're pre-filling the TextArea. Are you able to use an ASP.NET TextBox control with the multi-line properties set? If so, I'd use that and change your top code to just set the .Text property of the Textbox control.
Either way, in the Javascript (if this is jQuery), I'd do this:
if ($('review-text') && $('review-text').val() != "")
{
new yelp.ui.widget.DefaultValueTextField($('review-text'), 'Please write here');
}
That way the default value is only set if there is no text.
I changed the HTML of text area.And remove the class="form400 default_valued_text_field" attribute from its html.
myDoc = this.webBrowser1.Document;
myDoc.GetElementById("review-text").OuterHtml = "<textarea id=\"review-text\" onkeyup=\"javascript:ui.widget.countChars(this.form.comment,5000);\" onkeydown=\"javascript:ui.widget.countChars(this.form.comment,5000);\" name=\"comment\" rows=\"8\" cols=\"40\" style=\"height: 86px;\"></textarea>";
After that i set the inner text of this element
myDoc.GetElementById("review-text").InnerText = "Testing";

Click an HTML link inside a WebBrowser Control

C# Visual Studio 2010
I am loading a complex html page into a webbrowser control. But, I don't have the ability to modify the webpage. I want to click a link on the page automatically from the windows form. But, the ID appears to be randomly generated each time the page is loaded (so I believe referencing the ID will not work).
This is the content of the a href link:
<a
id="u_lp_id_58547"
href="javascript:void(0)"
class="SGLeftPanelText" onclick="setStoreParams('cases;212', 212); window.leftpanel.onClick('cases_ss_733');return false; ">
My Assigned</a>
Is the anyway to click the link from C#?
Thanks!
UPDATE:
I feel like this is close but it is just not working:
HtmlElementCollection links = helpdeskWebBrowser.Document.Window.Frames["main_pending_events_frame"].Document.GetElementsByTagName("a");
MessageBox.Show(links.Count.ToString());
I have tried plugging in every single frame name and tried both "a" and "A" in the TagName field but just have not had any luck. I can just not find any links; the message box is always 0. What am I missing?
Something like this should work:
HtmlElement link = webBrowser.Document.GetElementByID("u_lp_id_58547")
link.InvokeMember("Click")
EDIT:
Since the IDs are generated randomly, another option may be to identify the links by their InnerText; along these lines.
HtmlElementCollection links = webBrowser.Document.GetElementsByTagName("A");
foreach (HtmlElement link in links)
{
if (link.InnerText.Equals("My Assigned"))
link.InvokeMember("Click");
}
UPDATE:
You can get the links within an IFrame using:
webBrowser.Document.Window.Frames["MyIFrame"].Document.GetElementsByTagName("A");
Perhaps you will have to isolate the link ID value using more of the surrounding HTML context as a "target" and then extract the new random ID.
In the past I have used the "HtmlAgilityPack" to easily parse "screen-scraped" HTML to isolate areas of interest within a page - this library seems to be easy to use and reliable.

How can you block the image to appear on WebBrowser Control?

I have created a simple web browser using c# and I want to block the images to appear on WebBrowser control. Can you please show me a sample code for this? Thanks in advance.
You can remove image-tags from the source htm like this:
string content = new WebClient().DownloadString(#"http://www.google.com/");
string contentWithoutImages = Regex.Replace(content, #"<img(.|\n)*?>", string.Empty);
webBrowser1.DocumentText = contentWithoutImages;

Categories