twice moveToElement and click does not work properly

2,763 views
Skip to first unread message

jonas

unread,
Apr 21, 2015, 8:32:54 AM4/21/15
to seleniu...@googlegroups.com
Hi,

I have this procedure which works perfectly well under linux and Firefox 31.4.0 and Selenium 2.45. It also works fine under Windows with Firefox 24. But it behaves really weird under Windows 7 and Firefox 31.
The following action need to be done:
1-  go with mouse on the menu button (no click) -> a drop-down for selection opens
2- on this dropdown the mouse goes on the next item (no click) -> on the side of the first drop-down opens another dropdown
3) on the third drop down the mouse selects an item to click on

The selenium command, which works under linux and Firefox31:

        Actions actions = new Actions(driver);
       
WebElement compendia = driver.findElement(By.xpath("//span[text()='Compendia']"));

       
WebElement menu1 = driver.findElement(By.xpath("//span[text()='Protein Production']"));
       
WebElement menu2 = driver.findElement(By.xpath("//a[text()='Cell Lines']"));


        actions
.moveToElement(compendia).perform();
       
Thread.sleep(1000); // currently a random number (but definitely more than enough time to open the  new selection menu)
        actions.moveToElement(menu1).perform();
       
Thread.sleep(1000);
        actions.moveToElement(menu2).click().build().perform();

Now under windows7 FF31 the following happens:
It opens step 1 and step 2 normally, but then goes back to another selection in step 2 and opens its selection menu where then can not find the menu2 (step 3)

I have tried so many version with build/perform/click, with different pause time, as a single command etc. The locators are fine, but of course I also played with it a bit.

It never works under W7/FF31. But we can for the longest time now not run our tests with the new libraries Selenium 2.45 and FF31 under Windows.

In the FF profile I have turned on/off profile.setEnableNativeEvents(true); but no change.

Can someone help me? Or is this improved in the next update?
Jonas


Gopa Kishore Mindi

unread,
Apr 22, 2015, 3:01:12 AM4/22/15
to seleniu...@googlegroups.com
Hi Jonas,

Update the code like actions.moveToElement(compendia).build().perform();  If it does not work then try using clickAndHold(compendia).build().perform();

And let me if it does not work. :)

Thanks,

Gopa.

jonas

unread,
Apr 23, 2015, 5:31:36 AM4/23/15
to seleniu...@googlegroups.com
Hi Gopa,

Thank you for your reply. No unfortunately both of the ideas do not work:

Your first proposal:
actions.moveToElement(compendia).build().perform();
Thread.sleep(1000);
actions
.moveToElement(menu1).build().perform();
Thread.sleep(1000);
actions
.moveToElement(menu2).click().build().perform();

does not work unless you also put a build() to "menu1", and then it behaves as I described before: for menu2 it jumps back and does a different selection on menu1. The click lands on menu 1, but not on menu2.
With your solution 2, the same thing happens except, the it selects all the item on the way to menu1 (click and hold, when it needs only moveOver).

I have attached a video which shows the problem more visually.

I also played again by making the xpath even more specific (they were already unique before). No result: why does it do a different selection after manu2(step3)???

Thanks.
Jonas
moveToElement2.mp4

jonas

unread,
Apr 24, 2015, 3:35:35 AM4/24/15
to seleniu...@googlegroups.com
Anybody?
This seems to be really not working, and not working for some time now!

Here is another example of a page not working:
        
       
...
driver
.get("http://gazelle.ihe.net");
WebElement connectathon = driver.findElement(By.xpath("//li/a[text()='Connectathon']"));
WebElement current = driver.findElement(By.xpath("//li[@class='last expanded']/a[text()='Current Connectathons']"));
WebElement cleveland = driver.findElement(By.xpath("//li[@class='first leaf']/a[text()='Cleveland 2015 resources']"));

Actions actions = new Actions(driver);

actions
.moveToElement(connectathon).build().perform();
Thread.sleep(1000);
actions
.moveToElement(current).build().perform();
Thread.sleep(1000);
actions
.moveToElement(cleveland).click().build().perform();
Thread.sleep(10000);

The only difference here is that it can not jump to a different selection, as there are not that many. But it does not open the Cleveland page!

Please anybody?
Jonas


Brian Smith

unread,
Apr 27, 2015, 1:22:14 AM4/27/15
to seleniu...@googlegroups.com
First off, you should never use explicit wait statements like Thread.sleep(1000) as you can never depend on them for a multitude of reasons. You should always wait for something to occur before proceeding to the next step.

This is a better way to achieve your results. My example makes use of absolutely zero reusability, but I'm tired and you can always refactor this within a new test suite using a page class.

@Test
public void test() {
   
WebElement connectathonMenu = localDriver.findElement(By.xpath("//ul/li[2]/ul"));
   
WebElement currentMenu = localDriver.findElement(By.xpath("//ul/li[2]/ul/li[5]/ul"));
   
WebElement clevelandOption = localDriver.findElement(By.xpath("//ul/li[2]/ul/li[5]/ul/li[1]/a"));

    hover
(connectathonMenu);
   
new WebDriverWait(localDriver, 30).until(
           
ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[@class='last expanded']/a[text()='Current Connectathons']")));

    hover
(currentMenu);
   
new WebDriverWait(localDriver, 30).until(
           
ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[@class='first leaf']/a[text()='Cleveland 2015 resources']")));

    clevelandOption
.click();
   
new WebDriverWait(localDriver, 30).until(
           
ExpectedConditions.titleIs("NA Connectathon 2015 Resources | Gazelle")
   
);
}

public void displayMenu(WebElement element) {
   
((JavascriptExecutor)localDriver).executeScript("arguments[0].style.display = 'block'", element);
}




Brian Smith

unread,
Apr 27, 2015, 10:33:26 AM4/27/15
to seleniu...@googlegroups.com
I definitely meant for you not to use implicit wait methods, not explicit. I replied a bit late in the night.

jonas

unread,
Apr 27, 2015, 11:16:51 AM4/27/15
to seleniu...@googlegroups.com
Hi Brian,

Thank you for your answer and your solution. The wait event is not the problem in my case. I used it to understand better what happens.
Your solution works with the given example site and probably also with my original to test site. However, with java script I also can make the following code:
private void clickElement(WebElement element){
          ((JavascriptExecutor)localDriver).executeScript("arguments[0].click()", element);
}

Giving the second Webelement (menu2 or clevelandOption), it does not even have to be visible and therefore the mouse does not have to hover.

I wonder why the version with Action is not working. Especially, this clicking on other elements which are not defined by any locators after it seems to destroy the java object (Action), I do not understand and think it is a bug. Especially, as it works under Linux.

Thanks again.
Jonas

David

unread,
Apr 27, 2015, 5:26:04 PM4/27/15
to seleniu...@googlegroups.com
Well, if it fails in just a particular OS-browser configuration, this sounds like an environment issue. Might be related to native events and Firefox. Maybe on Win 7 should try use older or newer FF version if you're not restricted to FF31, as you did mention FF24 for Windows in general. 

An alternate approach you can try is using javascript simulation of hover/mouse over instead of Actions class, that might work for you. However, this approach doesn't work if the hover menu behavior on the page/site is implemented using CSS styling rather than javascript (mouseover) event. For CSS hover, you will need Actions class, or hack the element's CSS styling to trigger "hover" state, hacking by way of the DOM/javascript.

For javascript simulation of hover/mouse over see: https://code.google.com/p/selenium/issues/detail?id=2067#c64

pavankumar abvg

unread,
Apr 27, 2015, 11:19:24 PM4/27/15
to seleniu...@googlegroups.com
Jonas,

Try with he below code

actions.moveToElement(menu2).build().perform();
driver.findElement(menu2).sendKeys(Keys.Enter);

Thanks,
Pavan



On Tuesday, April 21, 2015 at 6:02:54 PM UTC+5:30, jonas wrote:

jonas

unread,
Apr 28, 2015, 2:33:31 AM4/28/15
to seleniu...@googlegroups.com
Hi Pavan,

This does not work as the menu2 has nothing to enter and will give a Element not visible exception. I tried it in order to see that maybe it does prevent this jumping to another menu entry. But doesn't!

Thanks anyway,
Jonas

jonas

unread,
Apr 28, 2015, 2:54:23 AM4/28/15
to seleniu...@googlegroups.com
Hi David,
Well, yes it is an environment issue as I have it running with Linux/FF31 or W7/FF24. But it makes sens for us to let the test run with a system which our users also use. In our case it is now: W7/FF31. FF31 is long time supported and came out a while ago. Hence, W7/FF31 is not an exotic combination, I would think.
I have a javascript execute solution, but I do not like it as it uses a combination of Action and JavaScript simulation. See above, where I basically do the first 2 steps with the Action" - moveToElement" and then wrap the click (menu2) with a javascript execution simulation.
Otherwise, I have failed in doing a proper javascript for hover:
<input type="hidden" name="j_id25" value="j_id25"/>
<div class="rich-ddmenu-label compendia-menu rich-ddmenu-label-unselect" id="j_id25:j_id26">
  <div class="rich-label-text-decor">
    <span class="CompendiaLink">Compendia</span>
  </div>
<div style="margin: 0px; padding: 0px; border: 0px; position: absolute; z-index: 100;">
</div>
This is the Compendia Element. 

Thanks,
Jonas


Abhishek Singh

unread,
Apr 28, 2015, 3:15:46 AM4/28/15
to seleniu...@googlegroups.com
HI Jonas,
Kindly try this code and let me know if this works for you.
I checked the same in my environment and it works smoothly..

WebDriver driver = new FirefoxDriver();
driver.get("http://gazelle.ihe.net");
Actions actions = new Actions(driver);
WebElement connectathon = driver.findElement(By.xpath("//li/a[text()='Connectathon']"));
actions.moveToElement(connectathon).build().perform();
WebElement current = driver.findElement(By.xpath("//li[@class='last expanded']/a[text()='Current Connectathons']"));
actions.moveToElement(current).build().perform();
WebElement cleveland = driver.findElement(By.xpath("//li[@class='first leaf']/a[text()='Cleveland 2015 resources']"));
cleveland.click();

Regards,
Abhishek

jonas

unread,
Apr 29, 2015, 3:42:16 AM4/29/15
to seleniu...@googlegroups.com
Hi Abhishek,

Yes this works. Unfortunately, not with my original to test page. The menu there is a bit more complex. However, it is a similar approach as I already have and also works for me: I did wrap the last click with a javascript snipet:
          ((JavascriptExecutor)localDriver).executeScript("arguments[0].click()", element);

However, around the time of the click, the focus on the menu still jumps to another entry (in the menu). I.e. a bug.
Thanks for your reply,
Jonas
Reply all
Reply to author
Forward
0 new messages