English 中文(简体)
Automated web browser?
原标题:
  • 时间:2009-11-16 01:37:43
  •  标签:
  • c#
  • 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").SetAttribute("", "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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签