Search code examples
javaselenium-webdriverselenium-chromedrivermouseeventmouseover

How to get the xpath


How to get xpath on given page for Dresses Category (image with Dresses text)

https://www.amazon.com/b/ref=af_spr22_L1_w_nav_1?pf_rd_r=5FE3DS056G2PHQZPGCQV&pf_rd_p=e8199fbf-0485-4dda-aa8b-cc7a20b4c115&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=merchandised-search-6&pf_rd_t=30901&pf_rd_i=7147440011&node=1040660

i tried this : Actions action = new Actions(driver);

WebElement clothestyle = driver.findElement(By.xpath("//span[@data-csa-c-id='8nt4i-t93vdn-vkyjb-b2c2aj']")); action.moveToElement(clothestyle).build().perform();

But NoSuchElementException is coming


Solution

  • Try to find more simple Xpath, your's seems be based on auto-generated data, so can expired soon. Try something like:

    //img[@alt='Dresses']/../../parent::a
    

    or

    //img[@alt='Dresses']/parent::div
    

    Also, if you need just open section, clicking element should be enough:

    WebElement clothestyle = driver.findElement(By.xpath("//img[@alt='Dresses']/../../parent::a"));
    clothestyle.click();