开发者

webBrower Button Click without element ID?

开发者 https://www.devze.com 2023-02-01 10:51 出处:网络
I\'m working on a login system for a forum and trying to make it work via a c# .net form. I need to programitically click the login button on the forum with a webBrower control. So far I have this.

I'm working on a login system for a forum and trying to make it work via a c# .net form. I need to programitically click the login button on the forum with a webBrower control. So far I have this.

webPage page = new webPage(); 
page.URL = txtURL.Text;
page.Load();  //Load the text from the specified U开发者_如何学JAVARL
WebBrowser browser = new WebBrowser();
browser.Document.GetElementById("navbar_username").SetAttribute("value", textBox1.Text);
browser.Document.GetElementById("navbar_password").SetAttribute("value", textBox2.Text);

HtmlElement el = browser.Document.All["btnI"];
if (el != null)
{
    el.InvokeMember("click");
}
else
{
    MessageBox.Show("There is an issue with the program");
}

The issue is that the login button on the page does not have an ID or any real information that can allow me to click on it. Does anyone have any suggestions? Here is the code for the login button.

<input type="image" src="images/loginbutton.png" class="loginbutton" tabindex="104" value="Log in" title="Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself." accesskey="s">


Can you just submit form? Are there more buttons on that page?


You have a accesskey attribute on the input button that will be unique within the page.

Loop through all input buttons and find the one with an accesskey attribute that is set to s. This will be your submit button.

With jQuery you would use the following in order to select this element:

$('input[accesskey="s"]')


You could use webBrowser1.Document.GetElementsByTagName("input")
than you could loop through every HTMLElement and look for element.innerText == "Log in"
than you'ld have your button...

0

精彩评论

暂无评论...
验证码 换一张
取 消