Xpath not working

the following code is not working, xpath is taken from Firebug

locater = By.xpath(“/html/body/font/div/div[2]/div/div/div/div[2]/div[3]/div[1]/div/div/a[1]”);
driver.findElement(locater).click();

What is the reason ? Is Xpath reliable ?

sskini76 Train Asked on March 17, 2016 in Selenium WebDriver.
Add Comment
1 Answer(s)

we should not use absolute Xpath that will leads to error in dynamic pages , i recommend u should use Random xpath along with

1. ancestor
2. decedent
3.parent
4.child
5.following
6.following-sibling and preceding -siblings

use these concept and try to  construct you own xpath

locater = By.xpath(“/html/body/font/div/div[2]/div/div/div/div[2]/div[3]/div[1]/div/div/a[1]”);
driver.findElement(locater).click();

solution :

use this Xpath : //a[Contains(text(), “you link text”)]

**** CSS is more effective and time saving in selenium automation over any browser -(*IE)

so please use CSS instead Xpath , in dynamic scenarios only  you can use Xpath ( // a[Contains(text(), strVar)]    where strVar= some dynamic values on page

Thanks,
Anbarasan J.
Selenium Automation – Cognizant tech solutions

Anbarasan Train Answered on March 22, 2016.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.