【发布时间】:2016-08-14 12:04:27
【问题描述】:
我是 odoo 和 xpath 的新手。在谷歌搜索和阅读堆栈中的所有类似问题之后。我找不到解决问题的方法:
如何使用 xpath 将 div 定位到另一个 div 中?
以下是我达到目标的所有失败尝试:
<?xml version="1.0" encoding="utf-8" ?>
<openerp>
<data>
<!-- Customize header -->
<template id="custom_header" inherit_id="website.layout" name="Custom Header">
<xpath expr="//div[@id='wrapwrap']/main/div[1]" position="attributes">
<attribute name="id">my_target</attribute>
</xpath>
<xpath expr="//div[@class='oe_structure oe_empty']" position="attributes">
<attribute name="id">my_target</attribute>
</xpath>
<xpath expr="//div[@id='wrapwrap']/main/following-sibling::div[1]/data()" position="attributes">
<attribute name="id">my_target</attribute>
</xpath>
<xpath expr="//div[@id='wrapwrap']/main/following-sibling::div[1]" position="attributes">
<attribute name="id">my_target</attribute>
</xpath>
</template>
</data>
</openerp>
我有错误:
元素“....”不能位于父视图中。
但是,我在另一个任务中使用了以下表达式,并且效果很好:
<?xml version="1.0" encoding="utf-8" ?>
<openerp>
<data>
<!-- Customize header -->
<template id="custom_header" inherit_id="website.layout" name="Custom Header">
<xpath expr="//div[@id='wrapwrap']/header" position="attributes">
<attribute name="id">my_header</attribute>
</xpath>
</template>
</data>
</openerp>
下面的结构取自 chrome 检查器:
提前感谢您的帮助。
【问题讨论】:
-
你在输入的任何地方都有
xmlns="..."吗?很难从屏幕截图中分辨出来。 -
只需点击图片即可全屏显示。还是你想发布一个xml代码而不是截图!!?
-
我全屏查看它,看起来
html标签被切断了。实际数据很有帮助。很难复制/粘贴屏幕截图并实际尝试在其上运行 xpath。 -
只是让您知道,使用 Chrome 调试器,您可以右键单击节点并转到 Copy -> Copy XPath。