I'm doing an automation program. I load a webpage into my windows form and load it in WebBrowser control. Then, I need to click on a link from the WebBrowser programatically. How can I do this? for example:
Google Me
Facebook Me
The above are 2 different conditions. The first element does not have an id attribute while the second one does. Any idea on how to click each programmatically?
You have to find your element first, by its ID or other filters:
HtmlElement fbLink = webBrowser.Document.GetElementByID("fbLink");
And to simulate "click":
fbLink.InvokeMember("click");
An example for finding your link by inner text:
HtmlElement FindLink(string innerText)
{
foreach (HtmlElement link in webBrowser.Document.GetElementsByTagName("a"))
{
if (link.InnerText.Equals("Google Me"))
{
return link;
}
}
}
You need a way to automate the browser then.
One way to do this is to use Watin (https://sourceforge.net/projects/watin/). It allows you to write a .Net program that controls the browser via a convenient object model. It is mainly used to write automated tests for web pages, but it can also be used to control the browser.
If you don't want to control the browser this way then you could write a javascript that you include on your page that does the clicking, but I doubt that is what you are after.
Related
I'm making an application that can find an item on a website if the item name and colour matches the one set inside the application.
If the item name is set as "backpack" and colour set as "green" the application should find a match on the page and click the link. The website is this: Click
I would prefer doing this in C# with http requests or something similar. I would also do PhantomJS if anyone has a better solution using it.
You can use selenium, it basically allows you to act like a user with actual web browser. http://www.seleniumhq.org/
You can do something like mentioned below with the help of XPath:
driver.Navigate().GoToUrl("http://www.supremenewyork.com/shop/all/bags");
var backpack= driver.FindElement(By.XPath("//*[contains(#class,'inner-article')]//h1//a[contains(., 'Backpack') or contains(., 'Backpack')]"));
var colorGreen = driver.FindElement(By.XPath("//*[contains(#class,'inner-article')]//p//a[contains(., 'Acid Green') or contains(., 'Acid Green')]"));
if (backpack.Text == "Backpack" && colorGreen.Text == "Acid Green")
colorGreen.Click();
It is a tested code, it successfully finds the required values within tags, clicks and moves to that page.
Hope it helps.
I'm programming in WPF/C# VS2012/2010. I was trying to make an application where you can click on a button to login to an account. The very first webbrowser i used was C# System.Windows.Forms.WebBrowser.
It was fine all methods was nice and simple to use:
Browser.Document.GetElementById("Email").SetAttribute("value", "xxx");
Browser.Document.GetElementById("signin").InvokeMember("Click");
or
HtmlElementCollection textArea = Browser.Document.GetElementsByTagName("textarea");
foreach (HtmlElement element in textArea)
{
if (element != null)
{
element.Focus();
element.InnerText = "Very nice :]";
}
}
This webbrowser is very simple to use, but it is not good enough: it crashed, doesn't use Active-X, HTML5, Silverlight, and much more... So the next one I was trying to use it was "Awensomium".
This is a good webbrowser, no crashes and can easily use all that things I described above, but it's not so simple to use, it doesn't have methods to click buttons, or anything and I can't figure out how I can do this.
Do you know some webbrowser search engine for WPF/C# that allows me to click button etc... and using Active-X,HTML5 and other technologies?
If you are developing in WPF, you shuld use System.Windows.Controls.WebBrowser instead of Forms.WebBrowser. It uses your installed instance of Internet Explorer, so features depend on your IE version. If you upgrade to IE9 you'll be able to show and handle html5,css3 .. items. But if you like Awesomium, then you should try this: http://wpfchromium.codeplex.com/ (there are some examples also) .
I am writing a simple personal app that has a browser control and I want it to automatically "Refresh" gmail to check it more often than it does by default. There are monkey scripts that do this but I'm trying to add my personal style to it.
Anyhow, I've looked around and found everything but what I can do in csharp using the browser control.
I found this:
// Link the ID from the web form to the Button var
theButton = webBrowser_Gmail.Document.GetElementById("Refresh");
// Now do the actual click.
theButton.InvokeMember("click");
But it comes back with null in 'theButton' so it doesn't invoke anything.
Anyone have any suggestions?
It's been awhile since I've used JavaScript, but given the other answers and comments that there is no real ID associated with the element, could you do something like the following:
Search all Div's with an attribute of Role == 'Button' and an InnerHtml == 'Refresh'.
Once the correct InnerHtml is found, get the Element.
Invoke the click on the found Element.
Again, this may be blowing smoke, but thought I'd throw it out there.
edit: Just realized you are doing this with C# and a browser control; however, the concept would still be the same.
The best suggestion I could give you at this point involves an existing API that is used for .NET web browser based automation:
http://watin.org/
Since the div tag with the desired button really only seems to identify itself with the class name, you could use the Find.BySelector(“”) code included with the most recent version of watin.
How can I build my own search function for the webbrowser control, and if possible remove the built-in one (CTRL + F find function, that is)? Any help regarding this would be appreciated.
Edit - How would I search for text and select it? Pretty much like how the find dialog will find text and select it, I want to do that and I have messed with the document property of the page but I still can't get it to work. Any help?
That is the purpose of the WebBrowser control.
There are third party browser controls you can use instead:
GeckoFX
Mono.WebBrowser
Update (following comments):
You can capture the KeyPress events of the control to find out if ctrl+f was pressed, then use your own search algorithm on the document returned by the control.
I expect you should be able to do something similar with the third party controls as well.
We are using the WebBrowser control in c# winforms and need to be able to get information about the Url the cursor is positioned on.
So we have a web page in design mode, which has multiple urls, when the cursor is over one I would like to call a method which would return the id of the link.
Thanks
You can use the IHTMLCaret to get the cursor position from there using IMarkupPointer you can get the element in the current scope.
The webBrowser control has a Document property which has a Links collection. Each Link is an HTMLElement which has events you can tap into. Again, I'm not sure what you mean "cursor" because in the web world, unless if you're in a textbox, there really isn't a "cursor" (which is what I meant to ask in my comment) but you can tap into the MouseOver event and other stuff like that.
Example:
foreach (HtmlElement element in this.webBrowser1.Document.Links)
{
element.MouseOver += (o, ex) =>
{
Console.WriteLine(ex.ToElement.GetAttribute("HREF"));
};
}
This will print out the actual URL that the mouse is over.
You can have a look at this article - Hosting a web browser component in a C# winform - which explains several ways to perform that. or go directly to this one - Hosting a webpage inside a Windows Form - Basically what you need to do is handle the Click of the DOM object inside the COM WebBrowser of IE. You achieve this by handling the Js events inside your C# code.
I remember this kind of customization must be done using the AxSHDocVw.AxWebBrowser COM object instead of the System.Windows.Forms.WebBrowser Class from the newer versions of the .Net Framework.
I could send you more data about this, I did it some project, just give me time to find it ;). In the mean time try with those links.
By!