不同浏览器的 xpath 是否不同? [英] Is xpath different for different browser?

查看:45
本文介绍了不同浏览器的 xpath 是否不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常有趣的问题.我有一个 xpath,通过它我可以检索价值.

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

例如.

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

在 firefox 和 Chrome 中,它给出相同的文本,而在 IE 中,它给出不同的文本.

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

各种浏览器的 xpath 之间是否有任何区别,或者是我没有遇到的其他问题.

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

推荐答案

阅读 Selenium 如何处理 Xpaths 此处.

Read up on how Selenium handles Xpaths here.

在 Chrome 和 Firefox 中,我右键单击同一个 DOM 元素(如此处),选择复制 Xpath",这就是我得到的:

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

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

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

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

(一个是带属性值,另一个(FF)是绝对路径,说明FF不理解Crhome生成的Xpath)

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

所以对于 Selenium 测试来说,它在浏览器之间很重要.(我没有在 IE 上测试)

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

我跑了这个

 @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();
        }

测试通过了.如果我将 Chrome 的 Xpath 复制粘贴到那里,它将失败.

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

这篇关于不同浏览器的 xpath 是否不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆