Reputation: 25
This is driving me crazy, I just need WebDriver to right-click on an element and the rest of my selenium will work.
I'm using Eclipse, all my .jar imports and libraries are set up correctly.
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://............/";
selenium = new WebDriverBackedSelenium(driver, baseUrl);
Much later in my code, I left-click inside the javascript portlet row I want to execute a right-click on without any problems.
selenium.click("//tr[@id[contains(.,'Equipment')]]");
That works just fine, the row that gets clicked is highlighted, as it is supposed to do.
Now I just need to right-click on it, but I can't! Trying:
selenium.contextMenu("//tr[@id[contains(.,'Equipment')]]");
fails to right-click, and returns me the error: java.lang.UnsupportedOperationException: contextMenu.
There is an element that is not considered 'visible' until that row is right-clicked. It is being detected as existing, but selenium will not run it unless I manually right-click it myself mid-run in WebDriver to make it visible. Otherwise, it just gives me this:
com.thoughtworks.selenium.SeleniumException:Element is not currently visible and so may not be interacted with.
Right-clicking on that row is what makes the element 'visible', which is why I need to have selenium right-click so badly. I have tested to make sure the not visible element is being detected as existing, it is.
boolean exists = selenium.isElementPresent("//a[@id[contains(.,'faction')]]");
boolean visible = selenium.isVisible("//a[@id[contains(.,'faction')]]");
System.out.println("Exists: " + exists);
System.out.print("Visible: " + visible);
Gives me
Exists: true
Visible: false
In other words, it's not a problem with my xpath. It's there. It's just not 'visible'. I've tried sending Shift+F10 as a replacement for right-clicking as well, no good.
This all works fine in Selenium IDE, but I need it working with WebDriver so I can mess with it in Java. Please help me out.
EDIT: Thanks to Slanec's advice and more messing around with the actions builder, I found that it's a problem with my selenium webdriver not being up to date for Firefox 14. It's odd, considering I thought I had downloaded the most recent one just last Thursday. Either way, apparently native event support for Firefox 14 was not added until webdriver 2.25.0, and I am using 2.24.1.
Upvotes: 2
Views: 5982
Reputation: 38424
Unfortunately, when looking at the source code, the contaxtMenu()
method has not been added to WebDriver emulation. It's not even present at the org.openqa.selenium.internal.seleniumemulation
package where all the other methods (disguised classes via the Command-pattern) sit. It was, most likely, not backported to WebDriverBackedSelenium
when it was introduced (Selenium RC is deprecated and not in active development, remember?).
Things you could try:
Get rid of Selenium RC, if you still can. The WebDriver API is much more clean, powerful, and actively developed :). To do it with WebDriver, you'd do
WebElement elem = driver.findElement(By.xpath("//tr[@id[contains(.,'Equipment')]]"));
new Actions(driver).contextClick(elem).perform();
Try to do just this task with the WebDriver object you created and then fall back to using Selenium again. The two lines of code above should possibly work when just thrown in between the Selenium code.
Implement it yourself. It's not that hard. Just look at the org.openqa.selenium.internal.seleniumemulation
package and the WebDriverCommandProcessor
class, add contextMenu()
method as described above. This will mean to make your own Selenium builds from the edited source code, but hey, why not.
Last resort - figure out what action exactly the hidden element is waiting for and simulate the event via fireEvent()
method. Doing the following should do the trick (if not, it's just waiting for a different event):
fireEvent("//tr[@id[contains(.,'Equipment')]]", "contextmenu")
As a side note, instead of the long
"//tr[@id[contains(.,'Equipment')]]"
or a little bit better
"//tr[contains(@id,'Equipment')]"
you could just write this:
"css=tr[id*='Equipment']"
Isn't it a little bit nicer and more readable? I'm really comfortable with XPath, too, but learning some basic CSS Selectors (which can do most, but definitely not all XPaths can do) isn't that scary...
Upvotes: 2