Automated web browser?
For learning purposes I would like to automate some parts in a browser game, currently I am trying to fill out some simple text boxes, without any luck though. I've created a WebBrowser component on my form, loaded the website via it and tried this.
webBrowser1.Document.GetElementById("citizen_name").SetAttri开发者_StackOverflow中文版bute("", "myname");
When I click my "fill out text box" button nothing happens. The HTML part looks like this:
<input type="text" name="citizen_name" id="citizen_name" value="" class="field" tabindex="1" />
I am talking about the eRepublik.com game, appreciate any help.
Try this:
HtmlDocument document = this.webBrowser1.Document;
document.GetElementById("citizen_name").SetAttribute("value", "myname");
I usually take the following approach:
var someElem = webBrowser1.Document.GetElementById("some_id");
if (someElem != null)
{
someElem.InnerText = "Some value";
}
Some textareas of advanced editors cannot have their value set this way. To handle them I do something like the following:
someElem.Focus();
Windows.Forms.SendKeys.SendWait("Some value");
For this kind of problems there are much more suitable environments.
The easiest to use is definitely userscripts.
What exactly do you want to learn? How to test web apps, or how to develop them?
You can use of course simple javascript that you include in your page, or better yet, using Greasemonkey , so you don't modify the "client" code.
But greasemonkey would only be an option for Firefox, Opera and Chrome. If you really need a full blown cross browser automation test suite, you could use Selenium IDE , that allows to record or script a series of interactions with a web page, that can be automatically run in any of its supported browsers.
精彩评论