Naveen Kumar
Naveen Kumar

Reputation: 35

Submenu click not working with actions

enter image description here

Not able to click on submenu in our application. As per the attached picture. I am trying to achieve below scenario steps:

  1. Click on Menu 1
  2. MouseOver on <Item 3>
  3. Click on Sub Item 2

I have tried below code and not able to click on Sub Item 2, Because the issue is when selenium is trying to click on Sub Item 2, the focus/cursor/mouseover changed from Item 3 to Item 1. And 2nd sub item present in Item 1 got clicked.

driver.findElement(By.linkText("Menu 1")).click();
WebElement item3 = driver.findElement(By.linkText("<Item 3>"));
Actions action = new  Actions(driver);
action.moveToElement(item3).pause(Duration.ofSeconds(1)).build().perform();
action.moveToElement(driver.findElement(By.linkText("<sub Item2>")))
.click().build().perform();`

Please help me with the solution to handle this situation.

Upvotes: 0

Views: 108

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193148

As per your steps :

  1. Click on Menu 1
  2. MouseOver on Item 3
  3. Click on Sub Item 2

Here is the pseudo code block :

driver.findElement(By.linkText("Menu 1")).click();
WebElement item3 = driver.findElement(By.linkText("<Item 3>"));
Actions action = new  Actions(driver);
action.moveToElement(item3).build().perform();
driver.findElement(By.linkText("<sub Item2>")).click();

Upvotes: 0

Murthi
Murthi

Reputation: 5347

You can make it as chained actions. It may work for you.

WebElement menu1 = driver.findElement(By.linkText("Menu 1"));
WebElement item3 = driver.findElement(By.linkText("<Item 3>"));

Actions action = new  Actions(driver);
action.click(menu1).moveToElement(item3).click(driver.findElement(By.linkText("<sub Item2>"))).build.perform();

Upvotes: 1

Related Questions