Context click in Selenium 2.2 - c#

Got problem with opening custom context menu using advanced user interactions API in Selenium 2 (http://code.google.com/p/selenium/wiki/AdvancedUserInteractions).
Here is C# code:
var driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.flickr.com/photos/davidcampbellphotography/4581594452/");
IWebElement photoDiv = driver.FindElement(By.Id("photo"));
Actions actions = new Actions(driver);
var context = actions.ContextClick(photoDiv).Build();
context.Perform();
Still this code doesn't open context menu but simply clicks on the image.
Still many other interactions work fine, like those at this blogpost.
I'm using Firefox 5 on Windows XP, Selenium version 2.2.
Thanks in advance for any suggestions how to make it work.
UPDATE:
However, the code from here does the same (left click instead of context click).
ILocatable loc = (ILocatable)photoDiv;
IMouse mouse = ((IHasInputDevices)driver).Mouse;
mouse.ContextClick(loc.Coordinates);
mouse.MouseMove(loc.Coordinates, 15, 15);
Looks like a bug.

Have you tried using a robot click, Although they're not the most efficient way of doing so, but as long as the browser window is the top window it'll click the co-ordinates you set
Robot robot = new Robot();
robot.mouseMove(650, 590);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Maybe this will help you :-)

This seems to be that bug.
It is stated in the user group here, though the bug is about double click.

Related

Selenium C# dropdown jumps back to original state

Currently I am working on a little project to verify some login accounts.
I run into this problem that when i select the provider Canal Digitaal and then click 'Login', the dropdown box jumps back to it's initial state. When doing manually its working fine but when automating with a UnitTest it doesn't. I simplified the code (without POM) and its still not working. Hopefully someone knows what my problem is. For a similar site its working fine but for this it aint.. This is my code now:
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://video.sport1.nl");
driver.FindElement(By.Id("login")).Click();
new SelectElement(driver.FindElement(By.Id("provider"))).SelectByValue("canaldigitaal");
driver.FindElement(By.Id("login")).Click();
The problem is that there are two elements on the page with ID = login. You can see that by using driver.FindElements(By.CssSelector("#login")).Count and it returns 2. One is the DIRECT INLOGGEN link (top right) and the other is Inloggen (red button). You are mistakenly clicking the DIRECT INLOGGEN link which resets everything. Try this
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://video.sport1.nl");
driver.FindElement(By.Id("login")).Click();
new SelectElement(driver.FindElement(By.Id("provider"))).SelectByValue("canaldigitaal");
driver.FindElement(By.CssSelector("div.login > #login")).Click();
I grabbed a parent DIV first div.login (DIV with class login) then found the A child (>) with ID login.

Selenium - Force click on non-visible element

I'm using C# Selenium.WebDriver.2.44.0
On some 3rd party site I'm trying to press an element and get this:
var myWebElement = Driver.FindElement(By.XPath("//a[.=' some value']
myWebElement.Click();
I get the element and on click I get this:
{"element not visible\n (Session info: chrome=39.0.2171.95)\n (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86_64)"}
The item is within some sub menu revealing after I successfully press the parent menu. Also put 5 seconds sleep to be safe that the accordion is well seen (and it is well seen in my eyes).
Question - on 3rd party site how can I force the click on this item?
You could try bringing it to the top by changing the z-index with driver.execute_script().
You could use the inbuilt functions and WebDriver waits
var myWebElement = Driver.FindElement(By.XPath("//a[.=' some value']
WebDriverWait wait = new WebDriverWait(Driver.FindElement, TimeSpan.FromSeconds(10));
var element = wait.Until(ExpectedConditions.ElementIsVisible(myWebElement));
Actions action = new Actions(Driver.FindElement);
action.MoveToElement(element).Perform();
myWebElement.Click();
The problem is that Selenium executes actions one after another, but something like "show a sub menu" is lost between actions.
You're likely going to have to use an Actions chain for this:
Actions action = new Actions(Driver);
action.MoveToElement(Driver.FindElement(By(ParentElementSelector)))
.click()
.MoveToElement(Driver.FindElement(By.XPath("//a[.=' some value'])))
.click()
.Build()
.Perform();
This will move to the parent element, click, then move to the element you wish to find, then click. It will perform all of these as one action, which should be able to click the sub menu element.
When an element is not visible u can try this...It works for me
IWebElement WEHiddenID = driver.FindElement(By.Id(""));
WEHiddenID.SendKeys(OpenQA.Selenium.Keys.Enter);

Selenium click link multiple times

I'm automating tests for our webapp in Selenium WebDriver for C#. One of our test scenarios identified an issue with clicking the save button multiple times resulting in multiple identical records.
The standard IWebElement.Click() causes Selenium to block until the page is fully loaded. That means by the time our second click comes around to executing, the postback has been performed and we're not on the form page anymore.
Does anyone know a means of 'manually' clicking an element that won't cause Selenium to block?
You could either wait for a predetermined amount of time for the page to load:
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
...or to be more dynamic and wait for your button to appear:
var driver = new WebDriver();
var wait = new WebDriverWait(driver, TimeSpan(0, 1, 0));
wait.Until(d => d.FindElement(By.Id("button"));
Source: https://stackoverflow.com/a/7811812/2006048
Selenium also has source code that is similar to the second method: http://selenium.googlecode.com/svn/trunk/dotnet/src/WebDriver.Support/UI/ExpectedConditions.cs
Let me know if it works out for you. I personally use these options with WatiN:
browser.WaitForComplete();
...or:
browser.WaitUntilContainsText("Text");
It's a shame Selenium does not have the first one.
If we use JavaScript to send our click events, Selenium will not be blocked and we can click multiple times. However, because our click triggers a page load, we cannot reference the element directly. Instead, we need to specify the location to click and then fire our click events.
Because our WebApp uses JQuery, I was able to use the code specified here:
How to simulate a click by using x,y coordinates in JavaScript?
So in the end our C# logic looks something like this:
IWebElement element = driver.findElement(By.id("foobar"));
Point point = element.Location;
IJavascriptExecutor jscript = (IJavascriptExecutor)driver;
jscript.executeScript("$(document.elementFromPoint(arguments[0], arguments[0])).click();", point.X, point.Y);
Although this sends the click event I'm not 100% sure that the element receives it; I'll run some experiments and see.
What you need to do is click via javascript. In java this is done like this:
IJavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.id("gbqfd")));
executor.executeScript("arguments[0].click();", driver.findElement(By.id("gbqfd")));
I actually imagine it is very similar, this will not block selenium and you should be able to chain a few on before the page comes back.
if this newer approach is too slow you might be quicker doing it all in js, e.g.
IJavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementById(id).click();");
executor.executeScript("document.getElementById(id).click();");

Selenium WebDriver SubMenu click not working

I am not able to Click on SubMenu item using selenium webdriver using c#.
I am using IE9 and FireFox 13.
I have tried Action Builder but it does not work.
It gives an error saying element cannot be clicked.
WebDriverWait Wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5));
IWebElement menu = Wait.Until((d) => webDriver.FindElement(By.Id("id1")));
IWebElement menuOption = Wait.Until((d)=>webDriver.FindElement(By.Id("ID2")));
Actions builder = new Actions(webDriver);
builder.MoveToElement(menu).Build().Perform();
Thread.Sleep(5);
//then click when menu option is visible
menuOption.Click();
I have used even javascript :
js.ExecuteScript("return $(\"a:contains('ID1')\").mouseover();"); // Mouse hove to main menu
webDriver.FindElement(By.Id("ID2")).Click();
Please give some solution to click on hidden elements
You could use Expected Conditions to wait for element being clickable after hovering above it (Thread.sleep() is almost always the bad choice. And 5 ms won't be enough.).
The docs for this class (ExpectedConditions in OpenQA.Selenium.Support.UI namespace) are broken as I can see them now, but if you can follow the Java code in the link above, here are the Expected conditions for Java - it's really almost the same in C#, too.
Instead of using the statement Thread.sleep(). You can try to click on the element after making sure that it is displayed.
After you get the WebElement you want to click on , check if it is displayed by using the isDisplayed() method within the ExpectedContition statement about which #Slanec is talking about in the above post.
By this you can make sure you will click on the element only after Wait.Until() returns true. i.e the menuOption is displayed.
I'm writing the code in java as I do not know C#. But I guess you can figure out what I'm trying to say -
new WebDriverWait(driver, 60).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver ) {
return driver.findElement(By.Id("ID2")).isDisplayed();
}
});
I hope that this helps you.

How to click ok button in alert dialog box using watin?

I am trying to automate one website, in my project after clicking on 'submit' button, one alert message with ok button will come. I want to click that ok button.
I tried with these two codes seperatly but these are not working
AlertDialogHandler AlertDialog = new AlertDialogHandler();
ie.AddDialogHandler(AlertDialog);
ie.Button(Find.ByValue("Submit")).ClickNoWait();
AlertDialog.WaitUntilExists();
AlertDialog.OKButton.Click();
ie.WaitForComplete();
ie.RemoveDialogHandler(AlertDialog);
var AlertDialogHandler = new AlertDialogHandler();
using (new UseDialogOnce(ie.DialogWatcher, AlertDialogHandler))
{
ie.Button(Find.ByValue("Submit")).ClickNoWait();
AlertDialogHandler.WaitUntilExists(50);
var message = AlertDialogHandler.Message;
AlertDialogHandler.OKButton.Click();
ie.WaitForComplete();
}
While using these two codes, I got the same exception 'dialog not available within 30 seconds'.
Any help will be deeply appreciated. Thank You :)
This has happened with me a couple of times when I had multiple browser windows open and I was trying it out.
The solution at that time was to close all instances of IE, close NUnit / VS and start over again, and it worked like a charm. However, I was using a ConfirmDialogHandler and not an AlertDialog Handler.
If that does not help you might want to try adding the following before you fire up your browser instance.
Settings.AutoStartDialogWatcher = true;
Settings.AutoCloseDialogs = true;
I'm not familiar with WatiN, but here's another SO post that might help:
WatiN seems to not find JavaScript alert
There is a way to perform this without watin:
See my post:
Need a way to perform auto confirm for firefox or any other browser popup's
this is slightly unrelated but AlertDialogHandler's don't work for Firefox but there is a workaround...
http://pastebin.com/ZapXr9Yf

Categories