SubMenu clicking issue in Selenium

183 views Asked by At

I have to click a submenu which is dynamic : (hover on)Menu ->Submenu appears

My selenium code is

element1 =wait.until(ExpectedConditions.elementToBeClickable(By.id("parent_26")));
builder1.moveToElement(element1).clickAndHold(element1).build().perform();
element2=wait.until(ExpectedConditions.elementToBeClickable(driver1.findElement(By.xpath(".//[@id='parentGroup_26']/div[1]/table/tbody/tr/td[1]/div/div[12]/div/a"))));
builder1.moveToElement(element1).clickAndHold(element1).moveToElement(element2).click(element2).build().perform();

During execution, element1 gets clicked and submenu blinks. After executing this there is no error but the element2(i.e submenu ) is not getting clicked.

I have tried using moveToOffset() also but did not work. Please help me out.

1

There are 1 answers

3
Subh On

I think you are getting into trouble because of clickAndHold method; as the name suggests and as per the use, it clicks (without releasing) at the current mouse location. Hence, the next method in line (as per your code), i.e., moveToElement(element2) is not working.

Please try the below code and see if that works for you :

For hovering over element1 and then clicking on element2 in one-go:-

WebElement element1;
element1 = wait.until(ExpectedConditions.elementToBeClickable(By.id("parent_26")));

WebElement element2;    
element2 = driver.findElement(By.xpath(".//*[@id='parentGroup_26']/div[1]/table/tbody/tr/td[1]/div/div[12]/div/a"));

//Hovering over element1 and then moving to element2 and clicking it; all in one-go
builder1.moveToElement(element1).moveToElement(element2).click(element2).build().perform();

OR

For hovering over element1 first and then clicking on the element2 after that:-

WebElement element1;
element1 = wait.until(ExpectedConditions.elementToBeClickable(By.id("parent_26")));

//Hovering over element1
builder1.moveToElement(element1).build().perform();

WebElement element2;
element2 = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath(".//*[@id='parentGroup_26']/div[1]/table/tbody/tr/td[1]/div/div[12]/div/a"))));

//Moving to element2 and clicking it.
builder1.moveToElement(element2).click().build().perform();

And, just a suggestion. Please try using relative xpaths instead of absolute one. It's much more efficient.