I have automated some test cases mentioned at https://www.techlistic.com/2019/08/automate-buy-product-using-selenium.html For second test case mentioned at given link, I have written code for mouse hover like this:

WebElement womanLink = driver.findElement(By.xpath("//*[@id=\"block_top_menu\"]/ul/li[1]/a"));
Actions action = new Actions(driver);
TestCase2 tc2 = new TestCase2(driver);

This piece of code is working absolutely fine but since I have used Page Object Model I don't know how can I return WebElement womanLink = driver.findElement(By.xpath("//*[@id=\"block_top_menu\"]/ul/li[1]/a")); from page object class.
I have tried this:

//page object class code
@FindBy(how = How.XPATH, using = "//*[@id=\\\"block_top_menu\\\"]/ul/li[1]/a")
WebElement womanLink;

public WebElement getWomanLink() {
        return womanLink;
//test class code
TestCase2 tc2 = new TestCase2(driver);
Actions action = new Actions(driver);

but it is giving error: org.openqa.selenium.json.JsonException: java.lang.reflect.InvocationTargetException
I have explored Google and YouTube, on YouTube I have found some videos of mouse hover but they are without page object model approach.
Found two similar problems on stackover flow, links:
but these problems didn't help to modify the code.
Since I have used Page object model approach, WebElement womanLink path must be present in page object class. Have tried multiple techniques but not getting how can I achieve this?
Note: If above code snippets are not enough, here is my GitHub repository link https://github.com/atif-dev/EcommerceBuyProduct-Automation- for whole code

  • Can you share full stack trace? – Alexey R. Jun 7 at 14:41
  • I'm guessing it's because of your xpath value "//*[@id=\\\"block_top_menu\\\"]/ul/li[1]/a" . Try to use "//*[@id='block_top_menu']/ul/li[1]/a" – Millie Anne Volante Jun 8 at 4:45
  • give your class , test code , including full stack trace as alexey mentioned – PDHide Jun 8 at 16:52
  • @MillieAnneVolante Thanks a lot. Yes you are right, my xpath value was wrong. I have changed the xpath value in page object class. Now action.moveToElement(tc2.getWomanLink()).build().perform(); is working absolutely fine. – Atif Iqbal Jun 9 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.