Can anyone help me in writing xPath for below image, I wrote one but it's not working.

enter image description here

My xPath is //li[@class='active']//a[@class='dropdown-button btn']

  • Add some more HTML code, starting from descent node (div) – Bharat Mane Jun 4 '18 at 16:07

If you don't mind switching to CSS selectors, use the following:

CSS selector: #nav-mobile .active a.dropdown-button.btn


Try with the above XPath.


Try using below XPath:

//a[@class='dropdown-button btn'];
  • Thanks For Comment Prasanna, i Tried That To Now its Not Working. – koushick Jun 4 '18 at 12:48
  • Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@class='dropdown-button btn']"} – koushick Jun 4 '18 at 12:48
  • Are you sure you're within the right context? Is the element maybe part of an iframe? Because the xpath is valid and should match, given that the element exists in your current context and at the time you call it. – Daniel Jun 4 '18 at 12:51
  • i Used This Code Only – koushick Jun 4 '18 at 12:57
  • WebElement e4=driver.findElement(By.xpath("//a[@class='dropdown-button btn']")); Actions a1=new Actions(driver); a1.moveToElement(e4).click().build().perform(); – koushick Jun 4 '18 at 12:57

Try below Xpath and share your feedback

//*[contains(@class,'active')]/descendant::a[@class='dropdown-button btn']

You can copy the xpath from the Chrome/Firefox inspect elements:

1- Put the mouse on the element you want to inspect
2- Right click, Inspect element
3- Down bar shows, Right clikc, copy, copy xpath



I think the li element gets active only when you hover or click it. Please try to hover/click on the parent element and then try searching for your XPath.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.