Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 产生相同结果的3个不同XPath之间的差异_Selenium_Xpath - Fatal编程技术网

Selenium 产生相同结果的3个不同XPath之间的差异

Selenium 产生相同结果的3个不同XPath之间的差异,selenium,xpath,Selenium,Xpath,我正在尝试查找元素 HTML: <html><head></head><body> <a href="https://www.google.com">Click Here!</a> </body></html> driver.findElement(By.xpath("self::node()/child::node()/child::body/child::a&quo

我正在尝试查找元素

HTML:

<html><head></head><body>
<a href="https://www.google.com">Click Here!</a>
</body></html>
driver.findElement(By.xpath("self::node()/child::node()/child::body/child::a")).click();    //Statement1
driver.findElement(By.xpath("/child::node()/child::body/child::a")).click();    //Statement2
driver.findElement(By.xpath("child::node()/child::body/child::a")).click(); //Statement3
我的理解:

在语句1中,它将初始上下文节点称为
self::node
。这里,我们将整个
HTMLDocument
作为
self::node()
传递。因此,我们的初始上下文节点被设置为完整的
HTMLDocument
(也称为文档根节点
(/)
)。因此,我们的初始上下文节点设置为文档根节点
(/)

在语句2中,它绝对将初始上下文节点称为文档根节点
(/)

但是在语句3中,没有提到初始上下文节点

那么,这是否意味着如果我们不提及初始上下文节点,那么默认情况下它会被设置为文档根节点
(/)


请帮助提高我的理解。

从“上下文”的角度来看,您的1和3条语句是相同的(
self
child
都是xpath轴)。两者都以文档的根作为上下文(在Selenium中称为
SearchContext

在这种情况下,上下文是root,因为您可以直接在驱动程序中查找元素。如果您将有一个
WebElement
,并尝试查找该元素中的元素,那么对于1和3个语句,您的上下文将不会是
root

下面是一些更详细的解释。

假设我们有
test.html
和以下内容:


测试是这样的:

@测试
公开无效测试(){
驱动程序。获取(“file:///path_to_page/test.hml");
WebElement a=driver.findElement(By.xpath(“html/body/a”);/(1,2)
System.out.println(a.getAttribute(“val”);
试一试{
System.out.println(driver.findElement(By.xpath(“body/A/B”))).getAttribute(“val”);//(3)
}捕获(无接触元素例外e){
System.out.println(“由于上下文是root,所以找不到主体/A/B”);
}
WebElement b=a.findElement(By.xpath(“b”);/(4)
System.out.println(b.getAttribute(“val”);
System.out.println(b.findelelement(By.xpath(“/html/body/A”)).getAttribute(“val”);/(5)
}
这里有几点值得注意的地方:

  • 尽管我们的文件有根
    A
    ,但浏览器会自动添加html标记以使html文件有效。因此,它添加了
    html
    节点和
    body
    节点
  • 考虑到前面的一点,我们从访问
    html/body/A
    开始,它具有
    上下文
  • 然后,我们确保使用路径
    body/A/B
    无法找到
    B
    ,因为上下文仍然是
    root
  • 然后我们可以看到,我们可以在先前定位的
    A
    的上下文中找到
    B
  • 最后一件事是我们在
    B
    的上下文中查找
    /html/body/A
    。尽管我们使用
    B
    作为搜索上下文,但我们仍然可以找到元素,因为我们从
    /
    开始路径,这意味着
    并忽略任何搜索上下文

  • 那么,这是否意味着在上述3条语句(Statement1、Statement2和Statement3)中,由于findElement()方法正在查找驱动程序中的元素,并且驱动程序与整个HTMLDocument关联,因此初始上下文节点设置为整个HTMLDocument,也称为文档根节点(/)对于所有3条语句?此外,如果要查看元素内部并从斜杠开始路径,则将忽略元素上下文并从根节点执行搜索,如果我们查看元素内部并且xpath以self::node()/child::node()开头,或者如果xpath以child::node()开头那么第二个findElement()方法的初始上下文节点将设置为第一个findElement()方法的元素节点
    如果我们查看的是元素内部,xpath以斜杠开头,那么第二个findElement()方法的初始上下文节点将设置为文档根节点
    请澄清..添加了一些示例。我想这涵盖了你可能感兴趣的所有案例。谢谢@Alexey R.如此清晰简洁的解释。很抱歉,我没有足够的声誉来投票支持答案。为什么需要一个
    self::node()
    和一个
    child::node()
    ?你的用例是关于什么的?