4

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);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
user9790882
  • 57
  • 1
  • 1
  • 9

2 Answers2

0

It may be trying to click the element before it appears. Try to use a web driver wait before you move to the subelement. ( Since it worked previously I guess this should be the issue )

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.LOCATOR("subelement")));

It'll look like,

Actions mouseHover = new Actions(driver);
mouseHover.moveToElement(ParentElement);

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.LOCATOR("subelement")));

mouseHover.moveToElement(subElement);
mouseHover.click(subElement);

Cheerz

Muditha Perera
  • 1,216
  • 8
  • 24
0

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I have tried both options and didn't work. Here is the stacktrace: java.lang.NullPointerException at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:882) at org.openqa.selenium.interactions.Actions.(Actions.java:68) at HoverShemes.Order_CreateANewOrder.setHoverSearch(Order_CreateANewOrder.java:40) at testClasses.HomeTests.Hovers_Test.getHoverSearch(Hovers_Test.java:14) – user9790882 Sep 18 '18 at 14:28
  • @user9790882 Checkout my answer update and let me know the status – undetected Selenium Sep 18 '18 at 15:12
  • i have extended the Base class in the Test class and still got the same error. Thanks – user9790882 Sep 19 '18 at 16:54
  • This works now. I found that I had WebDriver driver; in my test class and have removed this. Thank you – user9790882 Sep 20 '18 at 12:46
  • @user9790882 Upvote the answer if this/any answer is/was helpful to you for the benefit of the future readers. – undetected Selenium Sep 20 '18 at 12:52