function - What is the difeerence between parent function and child function in xpath of selenium
问题描述
URL==> http://demo.guru99.com/test/login.html I use the above url for testing purpose
i try to find the web element using parent and child function in xpath,but child and parent function give me an same result,i use the below command
parent function==> $x("//div[@class='container-fluid']//parent::div")
child function==> $x("//div[@class='container-fluid']//child::div")
Observe that i use the same web element and try to fing it's parent and child element but after running the above cmd i get the similar result Execute the cmd in cosole tab How can have same element as a parent as well as child for particular web element Please help in above and if not clear the question let me know i try to more describe the question Thank you in advance
解决方案
Breaking it down:
//div[@class='container-fluid']//parent::div
//
select the root and all of its descendantsdiv[@class='container-fluid']
from all of the nodes selected in step 1., select all childdiv
elements with an attributeclass
equal to'container-fluid'
//
select all nodes selected in step 2 and all of their descendantsparent::div
starting from the nodes selected in step 3, select all parents of those nodes that arediv
elements
//div[@class='container-fluid']//child::div
//
select the root and all of its descendantsdiv[@class='container-fluid']
from all of the nodes selected in step 1., select all childdiv
elements with an attributeclass
equal to'container-fluid'
//
select all nodes selected in step 2 and all of their descendantschild::div
starting from the nodes selected in step 3, select all children of those nodes that arediv
elements
So the reason you are getting the same result in both cases is because the div
you are locating is a parent of one of the nodes selected in step 3 and a child of one of the nodes selected in step 3.
If you are just trying to select the parent or child of the container-fluid
div, then get rid of the double slashes:
//div[@class='container-fluid']/parent::div
//div[@class='container-fluid']/div
The child::
axis is redundant most of the time and can be omitted in this case as I have done above.
推荐阅读
- linux - 从所有目录检查公共子目录中的文件计数
- github - Appveyor、GitHub、repo 中的多个项目
- c# - 中兴PDF417识别
- security - 如何在不访问互联网的情况下在沙箱中执行 Python 或 Go 脚本?
- c++ - 简单问题 - 如何从单独的“结构”中的“类”访问变量?
- javascript - 如何为 TypeORM 实体中的 @PrimaryGeneratedColumn 设置自定义默认唯一 ID 字符串?
- android - SecondActivity.class 文件未与以红色显示的意图对象链接
- c++ - 如何检查cpp中给定输入的数据类型?
- c# - 无法访问公共接口的属性
- docker - 访问 docker 容器内的其他 docker 服务时超时