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();
Related
I'm trying to automate Paypal withdrawals by using C# and Selenium. The application logs into Paypal using provided credentials and clicks on the 'transfer money' link, which then shows a pop-up (which looks to be an iframe). My problem is that I can't click on any of the elements in the pop-up, and I've tried every suggestion I could find.
Here is a screenshot of the form and the underlying html:
paypal form
I'm trying to click on the 'From' dropdown and among other things I've tried:
driver.FindElement(By.XPath("//*[#id=\"selection-container\"]/form/section/table/tbody/tr[2]/td/div[1]/div[1]")).Click();
and
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].hidden = false;", driver.FindElement(By.XPath("//*[#id=\"selection-container\"]/form/section/table/tbody/tr[2]/td/div[1]/div[1]")));
but either get and 'Unable to locate element' or 'Element not visible' errors. How do I get to the 'From' input element on the pop-up? (If you're using paypal, you could also log in and take a peek at the pop-up if needed).
You need to switch to the iframe first
IWebElement frame = driver.FindElement(By.TagName("iframe")); // locate the iframe element
driver.SwitchTo().Frame(frame);
driver.FindElement(By.XPath("//*[#id=\"selection-container\"]/form/section/table/tbody/tr[2]/td/div[1]/div[1]")).Click();
And to switch back
driver.SwitchTo().DefaultContent();
Try
[FindsBy(How = How.CssSelector, Using = "div[class$='source-dropdown']")]
public IWebElement _ddSource;
The '$' specifies the end of the attribute, in the case the end of the class is source-dropdown
at first you need to switch to that iframe. use the below code:
IWebElement frame = driver.FindElement(By.CssSelector("iframe[src ='/moneytransfer']");
driver.SwitchTo().Frame(frame);
now you can click on that pop up by using this cssSelector:
div[class$='source-dropdown']
We have automated a few test cases using the Ranorex automation framework for a Silverlight web application. These test cases involve clicking buttons in order to invoke certain messages on the screen. In order to grab the button on the screen, we first create an Ranorex button object and then point it to the appropriate element using Ranorexpath. Then, we use the RanorexButton.Click() event to click the button. However, this event is unreliable. It works sometimes and at other times the button is not clicked. When the button is not clicked, we have to run the test case again from the start. What are we doing wrong? If this is a known problem of ranorex, please suggest workarounds.
I was facing the same problem but I am able to resolve the problem by introducing a Validate.Exists(infoObject) just before the click. Please make sure that you pass infoObject of your button or any element in Validate.Exists API.
Example:
Validate.Exists(repo.MyApp.LoginBtnInfo);
var button = repo.MyApp.LoginBtn;
button.Click();
With regards,
Avinash Nigam
I haven't heard about such a problem with Ranorex yet, maybe this is just a timing issue.
You could add a Validate.Exists(yourButton) right before the click, this ensures that the click is performed after the button was successfully loaded.
If it is a WebElement you could also use the PerformClick() method instead of the normal Click() method.
There are also different methods which will ensure that the button is in the visible area and has focus, like the EnsureVisible() or the Focus() method.
You will find the available methods of the used adapter in the online API of Ranorex.
If the Button is not within the area you can see without scrolling, you can use a
var button = repo.Buttons.button1;
button.EnsureVisible();
button.Click();
In this way the button is forced to be watched.
It might as well be an issue with the xpath and element Id-s.
If you have changing element Id-s even when the page is navigated away from and moved back (for example we have this issue with SAP related components) you might need to make a more robust xPath path variable using regular expressions.
Try to find object and parts of the path that do not change (eg. "iFrame id="MainContent"" or "btn id="ID_XXXX_Search_Button"") - ofcourse this will only help if the issue is within this.
Ranorex Regular Expression info can be found here: http://www.ranorex.com/support/user-guide-20/ranorexpath.html#c3294
A quick example of what I'm talking about:
Let's say we have an input field that has a changing ID in it's name:
US_EL.ID_F2B49DB60EE1B68131BD662A21B3432B:V_MAIN._046A-r
And I know that the part in the Id that doesn't change is:
:V_MAIN._046A-r
I can create a path for this searching the element by the ending of the elements' id that doesn't change using regular expression:
/dom[#domain='test.example.com']//iframe[#'identifier']//iframe[#'identifier2']//input[#id**~'^**:V_MAIN._046A-r']
The bold part will specify to search for an input element with an Id that ends with ":V_MAIN._046A-r".
An issue that might arrise from this is if you have elements using partially the same names you might get multiple elements returned for the same path. So it's wise to add a few more certain points to the path (eg. "iframe[#'identifier2']") when this issue arrises.
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.
I'm doing an automation program. I load a webpage into my windows form and load it in WebBrowser control. Then, I need to click on a link from the WebBrowser programatically. How can I do this? for example:
Google Me
Facebook Me
The above are 2 different conditions. The first element does not have an id attribute while the second one does. Any idea on how to click each programmatically?
You have to find your element first, by its ID or other filters:
HtmlElement fbLink = webBrowser.Document.GetElementByID("fbLink");
And to simulate "click":
fbLink.InvokeMember("click");
An example for finding your link by inner text:
HtmlElement FindLink(string innerText)
{
foreach (HtmlElement link in webBrowser.Document.GetElementsByTagName("a"))
{
if (link.InnerText.Equals("Google Me"))
{
return link;
}
}
}
You need a way to automate the browser then.
One way to do this is to use Watin (https://sourceforge.net/projects/watin/). It allows you to write a .Net program that controls the browser via a convenient object model. It is mainly used to write automated tests for web pages, but it can also be used to control the browser.
If you don't want to control the browser this way then you could write a javascript that you include on your page that does the clicking, but I doubt that is what you are after.
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