From the WatiN website:
// Open a new Internet Explorer window and
// goto the google website.
IE ie = new IE("http://www.google.com");
// Find the search text field and type Watin in it.
ie.TextField(Find.ByName("q")).TypeText("WatiN");
// Click the Google search button.
ie.Button(Find.ByValue("Google Search")).Click();
// Uncomment the following line if you want to close
// Internet Explorer and the console window immediately.
//ie.Close();
The above sample works just fine. However, since I do not want to open up a browser window, I modified the above code to use MsHtmlBrowser:
// goto the google website.
var ie = new MsHtmlBrowser();
ie.GoTo("http://www.google.com");
// Find the search text field and type Watin in it.
ie.TextField(Find.ByName("q")).TypeText("WatiN");
// Click the Google search button.
ie.Button(Find.ByValue("Google Search")).Click();
The TypeText line is throwing an exception. Any idea what's wrong?
The MsHtmlBrowser is only there to find elements and read their attribute values. There is no support for clicking a link, typing text, firing events, no session state or any other way of interact like with a normal browser. So us it for scrapping only.
HTH,
Jeroen
Related
The click event is not firing in firefox but works ok in chrome.
The test fails with the error: "Element not found on page."
Below is the code and HTML for the button I want to click.
Browser.ElementClickById("ctl00_ContentPlaceHolderBody_lvProducts_ctrl0_ctrl1_btnAddProductToCart_input");
and inside the elementclickbyid i have:
driver.FindElement(By.Id(elementID)).Click();
HTML code is:
event
You could try working around with a Javascript click.
// declare JS executor
var executor = (IJavaScriptExecutor)Driver;
// locate the input
var input = Driver.FindElement(By.XPath("//input[#type='submit']"));
// execute JS to click
executor.ExecuteScript("arguments[0].click();", input);
I've seen cases where regular Click(); does not work across browsers -- these cases are rare, but using JS click usually works across multiple browsers when I run into this issue.
driver.findElement(By.xpath("//input[#type='submit']")).click();
i am sure you are trying to use browser class to keep your methods there, but try to use xpath not id. just use this code to click what you need. don't use page object model or anything else. don't save it in your browser class under click method. just in your main code use this code to click. and before to run it make sure that you have only one type submit. if its gonna show to you 2 types then use this code
driver.findElement(By.xpath("//input[#type='submit'][1]")).click();
number 1 says click to first submit if the button which you need second then follow the logic and change the number to 2
driver.findElement(By.xpath("//input[#type='submit'][2]")).click();
for better answer share your code class and also URL where you are trying to click button and also which element you are trying to click
When you compose an e-mail in Gmail, to send the email to people on your Contacts list you have to click 'To' which leads you to a pop-up box. Is there a way to automate the closing of this box? I'm using Selenium and C#.
I think you're likely running into problems because that modular window is within an iFrame, and you need to tell Selenium to specifically look into that iFrame before searching.
See handling-iframes-using-selenium-webdriver.
Unfortunately, Google seems to assign a new ID to this iframe every time it's created, so you can't rely on that, so you'll need to create an IWebElement for the iframe element and pass it into the .SwithTo().Frame() call.
Specifically, you could try this (I tested this out, and it worked for me).
//Get iFrame element and switch to it.
IWebElement selectContactFrame= driver.FindElement(By.CssSelector("iframe.KA-JQ"));
driver.SwitchTo().Frame(selectContactFrame);
//Find cancel button and click it.
IWebElement cancelButton = driver.FindElement(By.XPath("//div[text()='Cancel']"));
cancelButton.Click();
I wrote a simple code to submit a sign up form with Selenium. Before submit, driver should come from home page to sign up page.
var firefox = new FirefoxDriver();
firefox.Navigate().GoToUrl("http://mywebsite/home");
If I print firefox.Title, it shows me title of home page currectly
And in home page, there is a sign-up button. Sign up button link is bellow.
<a target="_blank" href="SignUp.jsp">Register Here</a>
To navigate to sign up page, I wrote a line:
firefox.FindElement(By.CssSelector("a[href='SignUp.jsp']")).Click();
After this, driver shows me the sign up page in new window of firefox browser. To navigate driver to the sign up I wrote firefox.Navigate();
Now If I print firefox.Title, it shows me title of home page again.
Please help me to find out problem. Thanks in advance.
You pretty much grabbing the same title since the you never switched to newly opened window
// Get the current window handle so you can switch back later.
string currentHandle = driver.CurrentWindowHandle;
// Find the element that triggers the popup when clicked on.
IWebElement element = driver.FindElement(By.XPath("//*[#id='webtraffic_popup_start_button']"));
// The Click method of the PopupWindowFinder class will click
// the desired element, wait for the popup to appear, and return
// the window handle to the popped-up browser window. Note that
// you still need to switch to the window to manipulate the page
// displayed by the popup window.
PopupWindowFinder finder = new PopupWindowFinder(driver);
string popupWindowHandle = finder.Click(element);
driver.SwitchTo().Window(popupWindowHandle);
// Do whatever you need to on the popup browser, then...
driver.Close();
driver.SwitchToWindow(currentHandle);
And, after switching to new window you should get new title.
However, this window handles process is utterly confusing to me. Selenium .Net bindings provide PopupWindowFinder class to handle windows.
Gratitude to JimEvans for his nice works and this
Use
firefox.SwitchTo().Window(handle);
where handle is one of instances found in firefox.WindowHandles. This will switch between the different window instances. You can find more information in the docs for IWebDriver.SwitchTo().
I've recently started to test and use WatiN, and i came across some of problems/errors that i cant really solve.
First ill describe what i'm doing with my tests ..
I'll go and get all the links in a specific div in a list ... so i have a link lists ... which im gonna loop thru ...
In the loop im gonna use a link.clicknowait() function, to open that link (it opens in a new ie tab) .. then i sleep thread for like few seconds and i close that browser by attaching that link (url) to a new browser and after that the browser is closed as im using it always with 'using (..)' statement.
So the first problem is that when the browser gets all the links and start to click them one of the links might lead not the right page, meaning like it can lead to a page that is saying page doenst exist anymore, so after that i cant close that page anymore ... how to solve this and attack ie to that not existing page?
It hasnt got fixed url ... anything that you can recommend?
Is there a method something like "Try attach to ...." because else i get an error if it tries to attack a browser to a link which doesnt exist.
Also is there anyway to check if the link in a links list is still the right one, cuz i also had an exeption few times that it couldnt click on that link because it was empty in the page ...
Or can i check something like link.trytoclick() and if an error and just ignores that link and goes further ???
And the last question after clicking alot of links and opening and closing browser i got an error outofmemoryexception .. how can it be, i've used 'using statement' which always closed browser when it was out of it ...
Thanks in advance for the help.
You can use the href to open the link in the same window of your watin session, after you verify the link you can go back to original page.
Some thing like this:
string href = browser.Link("link_id").GetAttributeValue("href"); //or your link from the collection
browser.GoTo(href);
//Perform you check
browser.Back();
To check if the link exists use:
session.browser.Back();
I am writing a simple personal app that has a browser control and I want it to automatically "Refresh" gmail to check it more often than it does by default. There are monkey scripts that do this but I'm trying to add my personal style to it.
Anyhow, I've looked around and found everything but what I can do in csharp using the browser control.
I found this:
// Link the ID from the web form to the Button var
theButton = webBrowser_Gmail.Document.GetElementById("Refresh");
// Now do the actual click.
theButton.InvokeMember("click");
But it comes back with null in 'theButton' so it doesn't invoke anything.
Anyone have any suggestions?
It's been awhile since I've used JavaScript, but given the other answers and comments that there is no real ID associated with the element, could you do something like the following:
Search all Div's with an attribute of Role == 'Button' and an InnerHtml == 'Refresh'.
Once the correct InnerHtml is found, get the Element.
Invoke the click on the found Element.
Again, this may be blowing smoke, but thought I'd throw it out there.
edit: Just realized you are doing this with C# and a browser control; however, the concept would still be the same.
The best suggestion I could give you at this point involves an existing API that is used for .NET web browser based automation:
http://watin.org/
Since the div tag with the desired button really only seems to identify itself with the class name, you could use the Find.BySelector(“”) code included with the most recent version of watin.