Problem logging in programatically to a website using WebBrowser Control - c#

I'm trying to login programatically to https://www.salesgenie.com/Account/LogOn using WebBrwoser control.
The problem is when I click on "Log On", the browser doesn't navigate to the next page in the LogonCompleted event.
HtmlElement userName = wBrowser.Document.GetElementById("username");
userName.SetAttribute("value", SomeUserName);
HtmlElement password = wBrowser.Document.GetElementById("password");
password.SetAttribute("value", SomePassword);
wBrowser.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(LogonPageLoaded);
wBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(LogonCompleted);
HtmlElement logonForm = wBrowser.Document.GetElementById("logon-submit");
logonForm.InvokeMember("click");
I think this is because the element "logon-submit" calls a JavaScript function or so.
Please, any help would be appreciated.

Check out your 4th line, you're setting userName again when you should be setting password
EDIT
Besides the problem above I'm guessing that you're trying to invoke the click too soon. The button is created using JavaScript so you have to wait a bit before clicking it. Unfortunately there's no event that you can listen for to determine when the JavaScript is done although you could test various properties probably. The safest thing is to probably just wait a couple seconds after load before invoking click.
The code below works for me (although I don't have a valid username and password). I've got one button called button1 and one webbrowser called webBrowser1. Once the page is visually loaded in the browser clicking the button on the form correctly invokes the click event in the browser.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser1.Navigate("https://www.salesgenie.com/Account/LogOn");
}
private void button1_Click(object sender, EventArgs e)
{
string SomeUserName = "Test";
string SomePassword = "Test";
HtmlElement userName = webBrowser1.Document.GetElementById("username");
Console.WriteLine(userName.GetAttribute("value"));
userName.SetAttribute("value", SomeUserName);
userName.RemoveFocus();
HtmlElement password = webBrowser1.Document.GetElementById("password");
Console.WriteLine(userName.GetAttribute("value"));
password.SetAttribute("value", SomePassword);
HtmlElement logonForm = webBrowser1.Document.GetElementById("logon-submit");
logonForm.InvokeMember("click");
}
}
}

Did you try using logonForm.click(); ? Without invoking any member ?

Related

Code keeps running to the point where it throws me an exception how do I break it?

So I have this application which will help me with my emailing for some of my customers and everything is working just fine until it gets to the last invoke.
It presses the button and logs me in but then it keeps running the code and loops back to the button click again which throws me an error since the button isnt there anymore due to the fact that we already logged in so the HtmlDocument isnt the same anymore.
I've tried to put a return; at the end but that didnt stop my code from running and im fairly lost at this point.
I've tried debugging it with breakpoints and it seems to be, as I mentioned loop through the code again.
Exception Message: System.NullReferenceException: 'Object reference
not set to an instance of an object.'
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NoTheme
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.Navigate("https://accounts.google.com/ServiceLogin#identifier");
}
private void button1_Click(object sender, EventArgs e)
{
HtmlDocument startDoc = webBrowser1.Document;
HtmlElement usernameElement = startDoc.GetElementById("Email");
HtmlElement loginBtnElement = startDoc.GetElementById("signIn");
usernameElement.SetAttribute("value", "theGmail#gmail.com");
loginBtnElement.InvokeMember("click");
webBrowser1.DocumentCompleted += WebBrowser1_DocumentCompleted;
}
private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlDocument startDoc = webBrowser1.Document;
HtmlElement usernameElement = startDoc.GetElementById("Passwd");
HtmlElement loginBtnElement = startDoc.GetElementById("signIn");
usernameElement.SetAttribute("value", "thePassword"); //this is where it throws the error
loginBtnElement.InvokeMember("click");
}
}
}

Automatically show string on form open

I have two forms, the main opens a second form prior to main loading.
On this secondary form I have a textbox to display the logged in username for Windows.
I want this username to show in the textbox when then form opens but currently it only displays when clicking into the textbox and pressing down the spacebar.
I have tried changing the onclick stuff but closet I got was having it load when the cursor went over it.
How can I have the textbox show the username onload or would a label be a better idea?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Chat
{
public partial class loginForm : Form
{
public loginForm()
{
InitializeComponent();
}
private void loginForm_Load(object sender, EventArgs e)
{
string userName = Environment.UserName;
usernametextBox.Text = (userName);
}
private void userloginbutton_Click(object sender, EventArgs e)
{
Application.Run(new chatForm());
Application.Exit();
}
private void usernametextBox_TextChanged(object sender, EventArgs e)
{
}
}
}
Thanks
Daniel
There are plenty of places where you can initialize the value of a textbox.
One common place would be the Form's constructor.
Something like
private void Form2_Load(object sender, EventArgs e)
{
string userName = Environment.UserName;
usernametextBox.Text = (userName);
}
That would be the constructor of the second form, the one that contains the text box.
You can also pass the value to the second form via a constructor parameter or public function if you want the first form to be in charge of prepopulating the username value of the second form after creating it.
private void Form_Load(object sender, EventArgs e)
{
string userName = Environment.UserName;
usernametextBox.Text = userName;
}
if you want to load username on form loading then you should go with FormLoad event of form
or
if you want to load username on focus of textbox then you should go with GotFocus event

button in c# not firing

I am writing a simple code it has 3 buttons 2 that will need to open up other forms and one to close the program. When i start the program the exit button will not work even though i have it coded the same as any other time i have wrote a program.
When i press any of the buttons nothing happens. Im not 100% sure how to use the buttons to open another form but i know the exit button should work as is.
Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _3343_ParksJ_Lab02
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void workerButton_Click(object sender, EventArgs e)
{
WorkerForm workersForum = new WorkerForm();
}
private void suppervisorButton_Click(object sender, EventArgs e)
{
SupervisorForm workersForum = new SupervisorForm();
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
You have to subscribe your buttons' click events to the event methods before they'll fire correctly.
You can check the Designer.cs file to see if this has already been done, though I'm guessing it hasn't. You'll be looking for something like this:
this.workerButton.Click += new System.EventHandler(this.workerButton_Click);
One way to do so is directly in the constructor:
public MainForm()
{
InitializeComponent();
workerButton.Click += workerButton_Click;
suppervisorButton.Click += suppervisorButton_Click;
exitButton.Click += exitButton_Click;
}
Normally, I'd do this through the designer. Select each Button in turn, then open the properties panel and double-click on the event you wish to subscribe to, which will create the corresponding event method in the code-behind for you.
Look at .Designer.cs file and make sure your button is adding the correct delegate method. In your case it should exitButton_Click.
Sometimes when you change names of a button VS designer does not make the name change correctly in the .Designer file. Very rare but it happens.

Program in C# to read a file:Button not working

Here is my code, for some reason button two doesn't fire, button one does and when I place the code from button 2 into one it works there. What am i missing about the syntax to get buttons one and two to both work on click? I am about 2 weeks into learning c# so this is all new to me, I do not see why this code should not work.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
string filePath = null;
public Form1()
{
InitializeComponent();
}
//Method to check database connection
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("button1.Click was raised.");
}
//Method to select a file
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
if (file.ShowDialog() == DialogResult.OK)
{
filePath = file.FileName;
}
}
}
}
I assume that the event handler isn't subscribed (anymore). So have a look at the partial class of Form1 in the auto generated file Form1.Designer.cs. There must be somewhere this:
this.button1.Click += new System.EventHandler(this.button1_Click);
// is this missing?
this.button2.Click += new System.EventHandler(this.button2_Click);
How to: Subscribe to and Unsubscribe from Events (C# Programming Guide)
Make sure button2 is bound.
From the designer, select the button then go to the properties window. Click the lightning bolt, and make sure the click event is bound to button2_Click.
Alternative method is right-clicking on InitializeComponent() and select "Go to definition" (brings you to Form1.designer.cs) and look for the following:
button2.OnClick += new EventHandler(button2_Click);
If you've confirmed it's bound, we'll need to see more than what you've shown to determine the problem.

Why lose webbrowser control when adding click event handler?

I'm using .NET visual C# 2008 and in my webbrowser, I added click event handler to capture what html tag name I've clicked.
But then I lost control to the webbrowser.
When I start the application, in the webbrowser, I cannot enter texts into text field in the browser. It still accepts mouse clicks to the links though.
I've found few people having this issue in forums but no solution is found.
What am I doing wrong?
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace IERecorder
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private mshtml.HTMLDocument doc = null;
private void Form1_Load_1(object sender, EventArgs e)
{
txtRecord.Items.Add("start...");
txtRecord.Items.Add("start2...");
webBrowser1.Navigate("http://www.google.com");
}
private void webBrowser1_DocumentComplete(object sender, WebBrowserDocumentCompletedEventArgs e)
{
txtRecord.Items.Add(e.Url.ToString() + " loaded...");
if (doc == null)
{
doc = (mshtml.HTMLDocument)webBrowser1.Document.DomDocument;
mshtml.HTMLDocumentEvents2_Event iEvent;
iEvent = (mshtml.HTMLDocumentEvents2_Event)doc;
iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(ClickEventHandler);
}
}
private bool ClickEventHandler(mshtml.IHTMLEventObj e)
{
txtRecord.Items.Add("clicked ==>" + e.srcElement.tagName);
txtRecord.Items.Add("clicked2 ==>" + e.srcElement.getAttribute("name", 0));
txtRecord.Items.Add("clicked3 ==>" + e.srcElement.innerHTML);
return true;
}
}
}
Its a framework related issue, It will not work on framework 3.5. Change the project framework to 4.5.2, It will work.
I don't see a text field in your code? Did you mean the google search box?
This is a screenshot of what I was able to reproduce with the code that you supplied. I was able to search for stackoverflow and browse to it. I would doublecheck that your event handlers are still wired up correctly.

Categories