Webbrowser control document to fill form with button - c#

I know this is a similar question to others out there but I just can't get mine to work.
I have a login form I am trying to auto fill in a webbrowser control.
I want it to be able to fill the form from strings that are set from 2 listboxes when a button is clicked on the form outside the webbrowser.
Here is what I have so far but it does nothing on the browser control when I click it.
private void button2_Click(object sender, EventArgs e)
{
string user = listBox1.SelectedValue.ToString();
string pw = listBox2.SelectedValue.ToString();
webBrowers1.Navigate("http://xxxxxxx.com/");
System.Windows.Forms.HtmlDocument document = this.webBrowser1.Document;
document.All["username"].SetAttribute("value", user);
document.All["password"].SetAttribute("value", pw);
HtmlElementCollection el2 = webBrowser1.Document.GetElementsByTagName("button");
foreach (HtmlElement btn in el2)
{
if (btn.InnerText == "Sign in")
{
btn.InvokeMember("Click");
}
}
}
I must be missing something but cannot figure it out! Thanks for any help

Related

How to get Pop up window control item from c# page

I am working for a screen scrapping application from windows application
I can automatically navigate through login page and all the pages using the we browser methods and sometimes having to use the '.Click' to trigger buttons on some of the pages.
Here's the problem. When I do the final 'click' to get my data, web browser opens up a new explorer window(pop up windows) that contains the another link button and I have to do click on this link button using c# to get my final data.
How can I access the new window(pop up window) to scrape it?
I am using below code and this code open the URL in new pop up window.
HtmlElement toollinkbutton = WebBrowser1.Document.Window.Document.Body.Document.GetElementsByTagName("a")[48];
toollinkbutton .InvokeMember("click");
The new window may be due to target="_blank" or javascript and using InvokeMember will result in the new window opening. Add a handler to the WebBrowser control NewWindow event and handle the click by calling Navigate() instead.
private string url = "";
public Form1()
{
InitializeComponent();
WebBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
WebBrowser1.NewWindow += new System.ComponentModel.CancelEventHandler(webBrowser1_NewWindow);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection links = WebBrowser1.Document.Links;
foreach (HtmlElement var in links)
{
var.AttachEventHandler("onclick", LinkClicked);
}
}
private void LinkClicked(object sender, EventArgs e)
{
HtmlElement link = WebBrowser1.Document.ActiveElement;
url = link.GetAttribute("href");
}
void webBrowser1_NewWindow(object sender, System.ComponentModel.CancelEventArgs e)
{
WebBrowser webBrowser = (WebBrowser)sender;
HtmlElement link = webBrowser.Document.ActiveElement;
Uri urlNavigated = new Uri(link.GetAttribute("href"));
WebBrowser1.Navigate(url);
e.Cancel = true;
}

DocumentCompleted firing only once

I have a WebBrowser inside a form and I want to do some automation with it. I click a button inside a windows form that commands the Navigate method of the WebBrowser to a certain page. Then I automatically click a link after DocumentCompleted has fired but after that I want to also click a Button that exists in the new page that appeared by clicking the link. It seems DocumentCompleted fires only when I click the button in the windows form not when I automatically click the a link inside the webpage.
void BtnTestClick(object sender, EventArgs e)
{
webBrowser1.Navigate(#"https://play.google.com/apps/");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
}
public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var webBrowser = sender as WebBrowser;
//webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;
// test to see if we're on fist CONFIRM page then go forward by clicking
var links = webBrowser1.Document.GetElementsByTagName("a");
foreach (HtmlElement link in links)
{
if (link.InnerText == "Proceed anyway")
{
link.InvokeMember("click");
}
} // this works
webBrowser1.Document.GetElementById("gwt-uid-126").InvokeMember("click");
}
After the link.InvokeMember("click"); a new page loads in the webbrowser that has a button which I also want to click ( gwt-uid-126 )
But it doesn't get clicked.
I've also tried:
var elements = webBrowser1.Document.GetElementsByTagName("button");
foreach (HtmlElement file in elements)
{
if (file.GetAttribute("class") == "GKYRWGTDNX GKYRWGTDLY")
{
file.Focus();
file.InvokeMember("click");
}
}
With no luck!
From what I see, second click doesn't work because the document is not completely loaded and second click is invoked.
You will have to add another if-else block that handled second document load.
Edit1: I was on phone when I answered this, so couldn't provide any snippet. Following is the change that you can do WebBrowser_DocumentCompleted method.
var links = webBrowser1.Document.GetElementsByTagName("a");
foreach (HtmlElement link in links)
{
if (link.InnerText == "Proceed anyway")
{
link.InvokeMember("click");
}
}
// following is for the page that is loaded on click of link.
var gwt_uid_126 = webBrowser1.Document.GetElementById("gwt-uid-126");
if(gwt_uid_126 != null)
{
gwt_uid_126.InvokeMember("click");
}
You might want to check if the WebBrowser_DocumentCompleted method is actually being called on second page load. This might be the reason why second click is not registering.
move this part of code in the Constructor or Form_Load:
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
try this instead of using WebBrowserDocumnetCompletedEventHandler:
void btnTestClick(object sender, EventArgs e)
{
webBrowser1.Navigate(#"https://www.google.com/");
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
continue;
var webBrowser = sender as WebBrowser;
//webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;
// test to see if we're on fist CONFIRM page then go forward by clicking
var links = webBrowser1.Document.GetElementsByTagName("a");
foreach (HtmlElement link in links)
{
if (link.InnerText == "Proceed anyway")
{
link.InvokeMember("click");
}
} // this works
webBrowser1.Document.GetElementById("gwt-uid-126").InvokeMember("click");
}

C# reach click button in webbrowser?

I want to click event button in webbrowser object.
Button is clicked I want to reach out and write the code? For example, the login button is clicked on the webbrowser object in the pop-up on the site to capture the event and would like to write the code?
I write an example for you(how get event clicked button):
private void Form1_Load(object sender, EventArgs e)
{
//Or navigate to your url
webBrowser1.DocumentText = "<html><body><button id=\"btn1\" type=\"button\">Click Me!</button><button id=\"btn2\" type=\"button\">Click Me!</button></body></html>";
}
Call Click event:(when page loaded)
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.Click += new HtmlElementEventHandler(Document_Click);
}
Get Active Element When User click on document
void Document_Click(object sender, HtmlElementEventArgs e)
{
//Check Element is Button
if (webBrowser1.Document.ActiveElement.TagName == "BUTTON")
{
if (webBrowser1.Document.ActiveElement.Id== "your button id")
{
//Do someting
}
}
}
If I understand correctly, you're using the WebBrowser control in C# WinForms and you would like to fire an event when a button is clicked.
The control doesn't have a click event, but the Document does. To do this you can use WebBrowser.Document.Click event and then loop through the HTML elements to find the one that was clicked.
web.Document.Click += new HtmlElementEventHandler(Document_Click);
This link will help also: webbrowser-control-get-element-by-type

Not able to set Url property at runtime in webbrowser control

I have a webbrowser control in my Winform application.
Below regions belong to division of code sample.
Region 1
The current url page loaded is "http://MyWebsite.com". I am clicking a link (say "About Us") in the web page using code. This click will take me to new url page ("http://MyWebsite.com/About_Us"). In Navigating event I am recording this new url.
Region 2
Now I want to get all elements of this new url and click on a new link. But not sure how to do it. In Region 2 I am also assigning the new url to webbrowser object. But nothing reflects in the instance. webbrowser.url still contains the previous url path.
I have following code for button click:
private void Button1Click(object sender, EventArgs e)
{
// Region 1---------------------------------------------
HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("A");
foreach (HtmlElement link in links)
{
if (link.InnerText != null && link.InnerText.Equals("Click to view magic"))
{
link.InvokeMember("Click");
break;
}
}
// EndRegion---------------------------------------------
// Region 2---------------------------------------------
webBrowser1.Url = new Uri(_url.AbsoluteUri, UriKind.Absolute);
webBrowser1.Navigate(_url); //New Edit
links = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement link in links)
{
if ((link.GetAttribute("Name") == "BooHoo"))
{
link.InvokeMember("Click");
break;
}
}
// EndRegion---------------------------------------------
}
private void WebBrowser1Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
_url = e.Url;
}
Can anyone help me to do this. The question may not be very clear. Please let me know if you need any further details. Thanks.
So it was tricky a bit or else I am careless. I was watching the property values in debug mode. Later I noticed that, after pressing F5 in Visual Studio (continue debugging) and all the methods are run, webbrowser shows the changed values.
Hope it helps.
You need to subscribe Navigated event as WebBrowser works asynchronously.
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
--Do Something Here....
}

Get url of clicked hyperlink inside a Windows Forms Application WebBrowser

If someone clicks on a hyper link inside a WebBrowser control , what method can i use to get the url of that hyperlink and check if it has an attribute that tells the browser to open the link in a new tab/window. Windows Forms Application.
You could obtain the element that currently has user input focus using the Document.ActiveElement property.
private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
e.Cancel = true;
if (webBrowser1.Document != null)
{
HtmlElement currentElement = webBrowser1.Document.ActiveElement;
if (currentElement != null)
{
string targetPath = currentElement.GetAttribute("href");
//You can perform some logic here to determine if the targetPath conformsto your specification and if so...
MainForm newWindow = new MainForm();
newWindow.webBrowser1.Navigate(targetPath);
newWindow.Show();
//Otherwise
//webBrowser1.Navigate(targetPath);
}
}
}

Categories

Resources