C# jumping some textfields like searchboxes - c#

I'm just using watin to fill some textfields but sometimes it writes in the wrong textfield because the textfields name is not clear here's my code
IE browser = new IE(site);
browser.TextField(Find.By("type","text")).TypeTextQuickly(username.ToString());
browser.TextField(Find.By("type", //"password")).TypeTextQuickly(pass.ToString());
browser.Button(Find.By("type", "submit")).Click();

Edit your HTML code and setup ID´s for your input elements. Then use Find.ById

One option could be to see if there is an outer element (such as a Div) you can find first, and afterwards get the text fields from that element instead of from the browser variable. That could for instance look like this:
Div div = browser.Div(Find.ById("divId"));
//Div div = browser.Div(Find.ByClass("divClass")); // or like this for instance...
TextField text = div.TextField(Find.By("type", "text"));
TextField password = div.TextField(Find.By("type", "password"));
Button submit = div.Button(Find.By("type", "submit"));

I am unclear as to your knowledge of WatiN and testing so I will start from the begginging. First you need to go on the webpage you want to test and (in IE) go to tools -> Developer tools. Click the white arrow in the menu then proceed to click the box you wish to utilize. Once you do this the developer tools will give you the code for the textbox including many ways you can reference it in your code. For example, using dev tools in IE I can automate logging into my gmail like this:
IE browser = new IE("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=http://mail.google.com/mail/&scc=1&ltmpl=default&ltmplcache=2");
browser.TextField(Find.ByID("Email")).TypeText("myemail#email.com");
browser.TextField(Find.ByID("Passwd")).TypeText("mypassword");
browser.Button(Find.ByID("signIn")).Click();
There are many Find.By commands so there is no reason you can't acess ANY textfield you wish. You just need the html and to be specific to which one you want to write into. I hope this helps :)

Related

Programmatically Clicking on a Web Page's Button in Windows Forms Application

I am working on a project which is Analysis of Papers from Google Scholar. What I do is basically, parsing the HTML, storing related fields into database etc. However, I am stuck at a point, while I am taking the Titles of the publications, I realized, I am able to get first twenty elements. But, there are sixty papers in related account:
http://scholar.google.com/citations?user=B7vSqZsAAAAJ
So, I think as a solution, I need to click to the 'show more' button programmatically, so I can have all the Title's, Publication Venue etc.
What do you think? How can I perform that kind of action?
Edit: I checked the 'show more' button, while there is nothing to show as a next page, its html code still remains same. As a solution I can use loop for n times. However, I am looking for more robust solution.
Thank you for your time!
If it is clicking on a button within a WebBrowser control on a Windows Form Application, then 'Yes' you can do it.
There are ways of getting more control over identification by using XPath.
(You might need to use Javascript to use XPath for object interactions - since you haven't asked for that, I will assume you don't need it)
webBrowser.Navigate("http://www.google.com");
// Or
HtmlElement textElement = webBrowser.Document.All.GetElementsByName("q")[0];
textElement.SetAttribute("value", "your text to search");
HtmlElement btnElement = webBrowser.Document.All.GetElementsByName("btnG")[0];
btnElement.InvokeMember("click");
Or even typing into text boxes with
webBrowser1.Document.GetElementById("gs_tti0").InnerText = "hello world";
If its this website specifically, there is a simple workaround. Change the query string to what records you want.
http://scholar.google.com/citations?user=B7vSqZsAAAAJ&cstart=0&pagesize=2000

Other text input methods?

There are a bunch of websites with "search" input boxes.
If I input a word manually then it automatically search without need to click any buttons.
Using the following code:
GeckoInputElement input = (GeckoInputElement)geckoHtmlElement;
input.Value = "searchword";
The I see that search input box is filled but nothing happens automatically.
If I manually add space or any character then website works as expected.
Auto searches my wanted word.
I tried using input.Focus(); but still same.
Any ideas how I can input text into search box in a more advanced way or something like that?
I think the solution here is to manually trigger the event that triggers the search - for example the keyup event. I had the same problem which I solved by adding this code:
nsAStringBase eventType = (nsAStringBase)new nsAString("keyup");
var ev = browser.Document.CreateEvent("HTMLEvents");
ev.DomEvent.InitEvent(eventType, false, false);
nameBox.GetEventTarget().DispatchEvent(ev);
You would need to inspect your page and see if there are any javascript events attached to your input.
Good luck!

Checkbox in an email

I am creating an email using the c# MailMessage and I am trying to add a checkbox that doesn't need to be clicked. The checkboxes will be used for a checklist of what to bring to an event (like a packing list). I have:
MailMessage objEmail = new MailMessage();
objEmail.From = new MailAddress("sender#hotmail.com");
objEmail.To.Add(new MailAddress("example1#hotmail.com"));
objEmail.CC.Add(new MailAddress("example2#hotmail.com"));
objEmail.Bcc.Add(new MailAddress("example3#hotmail.com"));
objEmail.Subject = "Packing list!";
objEmail.IsBodyHtml = true;
objEmail.Body = #"<div width=""800px"">
<h3>WHAT TO BRING</h3>
<form>
<input type=""checkbox"" name=""item"" value=""shirt"">Shirt<br>
<input type=""checkbox"" name=""item"" value=""shoes"">Shoes
</form></div>";
but when I send the email the checkboxes do not appear in the list.
Output in outlook using outlook.com:
WHAT TO BRING
I have a bike
I have a car
Output in outlook using Microsoft Outlook:
WHAT TO BRING
[ ]I have a bike
[ ]I have a car
Output in outlook using hotmail.com:
WHAT TO BRING
I have a bike
[]I have a car
So the problem is with the mail client but it is inconsistent what the problem is. I s there any way to make a consistent output?
Is there a way with html that works to create the checkboxes or do I just need to include images of a checkbox?
Thanks in advance.
My guess is that their in inconsistency with the way the email clients handle check boxes.
Why not change each to [X] which is plain text so all email clients can see it.
Outlook.com has an issue to display multiple check or radio buttons within a single parent. / / / etc
add a table in there and add the 2 check boxes in a separate row and it should work fine.
weird Microsoft thinking
All email clients have their own rules on what's allowed. You may find that most providers don't allow JavaScript and just some allow positioning elements while some don't.
Your best bet is to use a plain text form or maybe an image of what the form looks like. And when I say image, don't mean make the entire thing an image, have images for each checkbox. Once you click anywhere in this form it opens up on your site to interact with it.
Furthermore, you really shouldn't be building html in your code like you are. Put that in an MVC view or a webforms partial and invoke the file from code. Using this approach you can still inject dynamic data into the template while putting the HTML where it belongs.

Click an HTML link inside a WebBrowser Control

C# Visual Studio 2010
I am loading a complex html page into a webbrowser control. But, I don't have the ability to modify the webpage. I want to click a link on the page automatically from the windows form. But, the ID appears to be randomly generated each time the page is loaded (so I believe referencing the ID will not work).
This is the content of the a href link:
<a
id="u_lp_id_58547"
href="javascript:void(0)"
class="SGLeftPanelText" onclick="setStoreParams('cases;212', 212); window.leftpanel.onClick('cases_ss_733');return false; ">
My Assigned</a>
Is the anyway to click the link from C#?
Thanks!
UPDATE:
I feel like this is close but it is just not working:
HtmlElementCollection links = helpdeskWebBrowser.Document.Window.Frames["main_pending_events_frame"].Document.GetElementsByTagName("a");
MessageBox.Show(links.Count.ToString());
I have tried plugging in every single frame name and tried both "a" and "A" in the TagName field but just have not had any luck. I can just not find any links; the message box is always 0. What am I missing?
Something like this should work:
HtmlElement link = webBrowser.Document.GetElementByID("u_lp_id_58547")
link.InvokeMember("Click")
EDIT:
Since the IDs are generated randomly, another option may be to identify the links by their InnerText; along these lines.
HtmlElementCollection links = webBrowser.Document.GetElementsByTagName("A");
foreach (HtmlElement link in links)
{
if (link.InnerText.Equals("My Assigned"))
link.InvokeMember("Click");
}
UPDATE:
You can get the links within an IFrame using:
webBrowser.Document.Window.Frames["MyIFrame"].Document.GetElementsByTagName("A");
Perhaps you will have to isolate the link ID value using more of the surrounding HTML context as a "target" and then extract the new random ID.
In the past I have used the "HtmlAgilityPack" to easily parse "screen-scraped" HTML to isolate areas of interest within a page - this library seems to be easy to use and reliable.

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").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.

Categories