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

2

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);
Sherrod answered 18/9, 2018 at 10:45 Comment(1)
Share the exception stack trace ?Zagreb
R
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:

Rameriz answered 18/9, 2018 at 12:41 Comment(5)
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.<init>(Actions.java:68) at HoverShemes.Order_CreateANewOrder.setHoverSearch(Order_CreateANewOrder.java:40) at testClasses.HomeTests.Hovers_Test.getHoverSearch(Hovers_Test.java:14)Sherrod
@Sherrod Checkout my answer update and let me know the statusRameriz
i have extended the Base class in the Test class and still got the same error. ThanksSherrod
This works now. I found that I had WebDriver driver; in my test class and have removed this. Thank youSherrod
@Sherrod Upvote the answer if this/any answer is/was helpful to you for the benefit of the future readers.Rameriz
P
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

Prasad answered 18/9, 2018 at 11:39 Comment(1)
Muditha Perera, i have tried your option and still geting the same error.Sherrod
R
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:

Rameriz answered 18/9, 2018 at 12:41 Comment(5)
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.<init>(Actions.java:68) at HoverShemes.Order_CreateANewOrder.setHoverSearch(Order_CreateANewOrder.java:40) at testClasses.HomeTests.Hovers_Test.getHoverSearch(Hovers_Test.java:14)Sherrod
@Sherrod Checkout my answer update and let me know the statusRameriz
i have extended the Base class in the Test class and still got the same error. ThanksSherrod
This works now. I found that I had WebDriver driver; in my test class and have removed this. Thank youSherrod
@Sherrod Upvote the answer if this/any answer is/was helpful to you for the benefit of the future readers.Rameriz

© 2022 - 2024 — McMap. All rights reserved.