0

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']

1
  • Add some more HTML code, starting from descent node (div) Jun 4, 2018 at 16:07

6 Answers 6

1

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

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

0
0
//ul[@id="dropdown1"]/preceding-sibling::a

Try with the above XPath.

0

Try using below XPath:

//a[@class='dropdown-button btn'];
6
  • Thanks For Comment Prasanna, i Tried That To Now its Not Working.
    – koushick
    Jun 4, 2018 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, 2018 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, 2018 at 12:51
  • i Used This Code Only
    – koushick
    Jun 4, 2018 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, 2018 at 12:57
0

Try below Xpath and share your feedback

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

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

Done!

0

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 and acknowledge you have read our privacy policy.

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