Automated web browser? - c#

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.

Related

Finding CSS selector path for Selenium C#

I am new to Selenium C# automation. Tried finding on web but did not get any help.
The html code looks like this. I need to find the element and then click it using CSS. The site only runs on IE.
<tbody>
<tr class="t-state-selected">
<td>Purchased</td>
<td class="">768990192</td>
I know web links can disappear, but here are a few I use when trying to figure out how to locate elements using Selenium's C# WebDriver:
https://automatetheplanet.com/selenium-webdriver-locators-cheat-sheet/
https://saucelabs.com/resources/articles/selenium-tips-css-selectors
https://www.packtpub.com/mapt/book/web_development/9781849515740/1
The bottom line is that you're selecting by id, class, or XPath. Each of these can be tested directly on the page using the F12 browser tools. For example, to find the first comment on your question above, you could try this in the console:
$x("//div[#id='mainbar']//tbody[#class='js-comments-list']/tr")
Here's another SO post with a quick and dirty answer.
And here is the official documentation from Selenium on how to locate UI elements.
To click on the number 768990192 which is dynamic we have to construct a CssSelector as follows :
driver.FindElement(By.CssSelector("tr.t-state-selected td:nth-of-type(2)")).Click();
You're really not giving us much info to work. I will try my best to accommodate. Even though the presented HTML is not enough to give an indication of the format and you've not presented any code of your current solution.
string url = "https://www.google.com";
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl(url);
driver.FindElement(By.XPath("//tr[#class='t-state-selected']")).Click();
This little code snippet.
Creates a internet explorer driver.
Goes to the url of your choice.
And then clicks the table row that has a class that equals "t-state-selected'. Which my guess is all or none of the table rows.

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

What is href="javascript:__doPostBack('ctl00$cph1$mnuPager','b3')">

I am coding a web browser control application that will perform some specific operations in a website.
There are, however, a couple pages of tables in the website, where transition in between is implemented with a Java Script command. Here is what I get when I use inspect element over one of the transition buttons:
<a class="ctl00_cph1_mnuPager_1" href="javascript:__doPostBack('ctl00$cph1$mnuPager','b2')"> 2 </a>
Given that I have already have the page in my c# application as follows:
HTMLDOCUMENT BrowserContent = webBrowser1.Document;
Can you give me any tips about how to programmatically click on that page transition button(ie. call that script).
I tried:
BrowserContent.InvokeScript("javascript:__doPostBack('ctl00$cph1$mnuPager','b2')");
But it didn't help.
Thanks a lot guys!
Try this:
BrowserContent.InvokeScript("javascript:__doPostBack('ctl00$cph1$mnuPager','2')")‌​;
This will invoke the script inside the double quotes.

C# jumping some textfields like searchboxes

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 :)

Open a new window with asp.net

Im new into that asp.net thing, but here goes.
I got at ImageButton, and when its clicked i want the image displayed in another window. If I can avoid using ajax i would like to do that.
If possible would like to make the window modal, but still avoid ajax, since Im not ready to mix more technolgies yet.
The existing answers with JavaScript are fine, but just to suggest an alternative - could you use a HyperLink (with an ImageUrl set so you still get an image) and set its Target property instead?
IMHO the best practice to show a picture is in the same page on the top of the content. I personally use Lightbox. You can find the documentation on their page, so it should be easy for you to integrate their JavaScript code.
Somewhat like this:
<asp:ImageButton ID="imbJoin" CssClass="btn-find" AlternateText="Find" ToolTip="Find" runat="server" ImageUrl="~/library/btn-find.gif" onClick="javascript:popUp("ServicesLocator.aspx")" />
Resource: http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_22832169.html
Using the ImageButton you need to use a JavaScript to open it in a new window. You can also look into the OnClientClick-event
You can use the ImageButton's OnClientClick property:
<asp:ImageButton ... OnClientClick="javascript:window.open('url_to_image');" >
But this popup window will not be modal.
The following javascript will do what you're looking for:
window.open('page.html','WindowTitle','width=400,height=200')
It might be worth pointing to a two relevant entries in the excellent EFNet's #javascript FAQ:
Correct Use of Popups - yay accessibility!
How do I make a popup window the same size as my image?
How do I create a custom 'OK' dialog or something similar? - modal windows are not that useful and something like the suggested Lightbox or similar scripts would be better "modal" options
Correct Use of Links - this one being only partly on-topic but the previous answers use of the "javascript:" pseudo protocol made it necessary: it is never required nor useful in a web page that should work across browsers. After all, JavaScript is the default (and only) scripting language.
Thank you for all the answers! I ended up using lightbox
I found this example
http://neutrongenious.wordpress.com/2007/09/08/lightbox-for-asp-net-2-0/
And it works perfectly

Categories