How to mouse hover a parent element and subsequently click on child element using Selenium and Action class

user9790882 :

I wrote a test to hover mouse on an Element which has a link underneath it and to click the subElement. I keep getting NullPointerException. It had work previously and stopped working again.

Actions mouseHover = new Actions(driver);
mouseHover.moveToElement(ParentElement);
mouseHover.moveToElement(subElement);
mouseHover.click(subElement);
DebanjanB :

As per your code attempts you havn't invoked the perform() method for Mouse Hover. You need to induce WebDriverWait for the elements and can use the following solution:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
//other lines of code
Actions mouseHover = new Actions(driver);
mouseHover.moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOf(ParentElement)))).perform();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath(subElement))).click();

Update

As you are still seeing the error as:

 java.lang.NullPointerException at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:882) at org.openqa.selenium.interactions.Actions.<init>(Actions.java:68)

This implies that WebDriver instance i.e. the driver is not accessible from this part of the code. The issue may be related to driver being null as you did not extend the Base class in the Test class. Ensure that the driver is accessible.

Related Discussions:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=84085&siteId=1