英文:
How to locate a div with non unique id/class, which contains a div with a certain text and then fetch all the <td> and <tr> tags under that div
问题
以下是翻译好的内容:
所以,我有类似以下的 HTML 代码。
<div class="generic classname" id="generic ID name"> // div1
<div class="presentation" id="body presentation"> // div2
<font>unique text</font>
<div class="generic classname" id="generic ID name"> // div3
// 在这个 div 下面,我有表格的条目。
// 多个 <td> 和 <tr>
</div>
</div>
</div>
我的工作是匹配 div 2 中的 "unique text",以便我可以定位元素 div 1,然后获取 div 3 中的所有表格
问题是,我不确定如何定位没有唯一类名或 ID 名称的 div。我不能使用完整的 XPath,因为表格会变化,div 也会随机变化。
英文:
So I have HTML something like this.
<div class="generic classname" id="generic ID name" > // div1
<div class="presentation" id="body presentation"> // div2
<font>unique text</font>
<div class= "generic classname" id="generic ID name""> //div3
// under this div I have the table entry.
// multiple <td> and <tr>
</div>
</div>
</div>
My job is to match the "unique text" in div no 2, so I can locate the element div number 1 and then fetch all the table <td> <tr> in div no 3
The problem is I am not sure how to locate the div which doesn't have a unique class name or id name. I can't use full XPath, since the table changes and the divs change randomly.
答案1
得分: 1
使用以下 xpath 关联到 div 2,查找唯一的文本,然后在该 div 中查找包含表格的 div。
//div[./font[text()='唯一的文本']]/div[1]/table
英文:
Use the below xpath with reference to div 2 to find the unique text and then find div and table inside div.
//div[./font[text()='unique text']]/div[1]/table
答案2
得分: 1
如果您所说的所有 div 都是嵌套的,而且您的目标是获取 div3 中的表格,您就不需要获取父元素。
这是一个选项:
//font[text()='unique text']/following-sibling::div
此 xpath 查找具有您唯一文本的 font,然后找到它的同级元素(相同父级)div。
此 xpath 标识符是另一个选项:
//font[text()='unique text']/parent::*/div
此 xpath 查找具有您唯一文本的 font,然后获取它的 *(任意)父级,然后获取其中的相关 div。
如果您想要获取 "div1",也可以再次使用 parent 轴向向上。
在开发工具中如下所示:

这是基于您的 HTML 外观:
<div class="generic classname" id="generic ID name">
<div class="presentation" id="body presentation">
<font>unique text</font>
<div class="generic classname" id="generic ID name">
<!-- 在此 div 下,我有表格条目。 -->
<!-- 多个 <td> 和 <tr> -->
</div>
</div>
</div>
不同的 HTML 将需要不同的 xpath,请告诉我是否需要更新。
英文:
If all your divs are nested as you say and your aim is to get the table in div3, you don't need to get the parent.
This is one option:
//font[text()='unique text']/following-sibling::div
This xpath finds the font with your unique text then it's sibling (same parent) div
This xpath identifier is another option:
//font[text()='unique text']/parent::*/div
this xpath finds the font with your unique text then get it's * (any) parent then gets the relevant div inside it.
You can do the parent axes to up again if you want "div1".
This is based on your html looking like:
<div class="generic classname" id="generic ID name" >
<div class="presentation" id="body presentation">
<font>unique text</font>
<div class="generic classname" id="generic ID name"">
// under this div I have the table entry.
// multiple <td> and <tr>
</div>
</div>
</div>
Different HTML would need a different xpath so please say if you need to update.
答案3
得分: 1
根据HTML:
<div class="generic classname" id="generic ID name" > // div1
<div class="presentation" id="body presentation"> // div2
<font>unique text</font>
<div class= "generic classname" id="generic ID name"> //div3
// 在此div下有表格条目。
// 多个 <td> 和 <tr>
</div>
</div>
</div>
由于您的用例不依赖于任何<div1>属性,您可以轻松地不考虑<div1>。
解决方案
要定位第三个<div>,您有以下四种方法:
-
使用文本
unique text和<div>属性://font[text()='unique text']//following::div[@class='generic classname' and @id='generic ID name'] -
使用文本
unique text和 index://font[text()='unique text']//following::div[1] -
使用具有子元素
<font>,其文本为unique text和<div>属性的<div2>://div[./font[text()='unique text']]//following-sibling::div[@class='generic classname' and @id='generic ID name'] -
使用具有子元素
<font>,其文本为unique text和 index 的<div2>://div[./font[text()='unique text']]//following-sibling::div[1]
英文:
As per the HTML:
<div class="generic classname" id="generic ID name" > // div1
<div class="presentation" id="body presentation"> // div2
<font>unique text</font>
<div class= "generic classname" id="generic ID name"> //div3
// under this div I have the table entry.
// multiple <td> and <tr>
</div>
</div>
</div>
As your usecase is not dependent on any of the <div1> attributes, you can easily avoid considering <div1>.
Solution
To locate the third <div> you have four approaches as follows:
-
Using the text
unique textand<div> attributes://font[text()='unique text']//following::div[@class='generic classname' and @id='generic ID name'] -
Using the text
unique textand index://font[text()='unique text']//following::div[1] -
Using the
<div2>which have a child<font>tag with text asunique textand<div> attributes://div[./font[text()='unique text']]//following-sibling::div[@class='generic classname' and @id='generic ID name'] -
Using the
<div2>which have a child<font>tag with text asunique textand index://div[./font[text()='unique text']]//following-sibling::div[1]
答案4
得分: 0
我会搜索或者使用 JavaScript 获取 "unique text" 的标签。
<script>
// 获取元素
let elem = document.getElementsByName('font');
</script>
https://www.w3schools.com/jsref/met_doc_getelementsbyname.asp
英文:
I would search or get the tag of the "unique text" with js.
<script>
// get the element
let elem = document.getElementsByName('font');
</script>
https://www.w3schools.com/jsref/met_doc_getelementsbyname.asp
答案5
得分: -1
一个更多的选项供您选择:
//div[font='独特文本']/div
英文:
One more option for you:
//div[font='unique text']/div
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
- java
- selenium
- selenium-webdriver
- xpath
- xpath-1.0


评论