Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is xpath different for different browser?

I am having a very funny issue. I am having a xpath through which i am retrieving value.

Ex.

System.out.print(driver.findElement(By.xpath("//*[@id='error-box']/ul/li")).getText().toString());

In firefox and Chrome it is giving same text while in IE it is giving different text.

Is there any difference between xpath of various browser or it is some other issue which i am not getting.

like image 329
OPTIMUS Avatar asked Apr 14 '14 06:04

OPTIMUS


People also ask

Is XPath same for all browsers?

Xpath is same across all browsers.

Which XPath does chrome use?

No, Chrome uses XPath 1.0. You can simplify your XPath expression to just a v2.

What are the 2 types of XPath?

There are two types of XPath: Absolute XPath. Relative XPath.

Does Selenium support all browsers?

Through WebDriver, Selenium supports all major browsers on the market such as Chrome/Chromium, Firefox, Internet Explorer, Edge, and Safari.


1 Answers

Read up on how Selenium handles Xpaths here.

In Chrome and Firefox, I right-clicked on the same DOM element (as described here), selected "copy Xpath" and this is what I got:

Chrome: //*[@id="js-pjax-container"]/div2/div2/form/button

Firefox (with Firebug): /html/body/div[4]/div2/div2/div2/form/button

(one is with attribute value and the other (FF) is the absolute path, which demonstrates that FF doesn't understand Crhome generated Xpath)

So for Selenium test purposes, it matters between browsers. (I didn't test on IE)

I ran this

 @Test
        public void testGitHubButton(){
        WebDriver driver = new FirefoxDriver();
        driver.get("https://github.com/");
       String signup = driver.findElement(By.xpath("/html/body/div[4]/div[1]/div[1]/div[1]/form/button")).getText();
           Assert.assertEquals("Testing for string equality", "Sign up for GitHub", signup );
        driver.close();
        driver.quit();
        }

And the test passes. If I copy paste Chrome's Xpath in there, it will fail.

like image 109
Andrejs Avatar answered Oct 13 '22 18:10

Andrejs