如何根据多个子div查找父div的xpath?

huangapple go评论71阅读模式
英文:

How to find xpath of a parent div based on multiple children divs?

问题

考虑以下示例HTML:

<div class="randomclass-parent" id="需要找到这个div">  // 我要找的父div
<table class="rando table">
<tbody role="presentation">
<tr role="presentation">
<td id="radnom">
<div class="childclass">Sam</div>
<td id="radnom">
<div class="childclass">Elliot</div>
<td id="radnom">
<div class="childclass">Smith</div>

</tbody>
</table>
</div>

现在,我知道父div包含一个table,其中包含多个表格行,这些行又包含带有预先知道的一些文本的表格数据标签。我想要能够像这样做。不确定其语法是什么。

String xpathofParentDiv = "父div,其中包含"//div[contains(text(),'Sam')] and //div[contains(text(),'Elliot')] 和 //div[contains(text(),'Smith')]";

我该如何根据位于表格数据<td>内的子div的文本来定位父div呢?

英文:

Consider this html for example

&lt;div class=&quot;randomclass-parent&quot; id=&quot;Need to find this div&quot;&gt;  // Parent Div I am looking for
&lt;table class=&quot;rando table&quot;&gt;
&lt;tbody role=&quot;presentation&quot;&gt;
&lt;tr role=&quot;presentation&quot;&gt;
&lt;td id=&quot;radnom&quot;&gt;
&lt;div class=&quot;childclass&quot;&gt;Sam&lt;/div&gt;
&lt;td id=&quot;radnom&quot;&gt;
&lt;div class=&quot;childclass&quot;&gt;Elliot&lt;/div&gt;
&lt;td id=&quot;radnom&quot;&gt;
&lt;div class=&quot;childclass&quot;&gt;Smith&lt;/div&gt;

&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;

Now, I know that the parent div contains a table which contains multiple table rows, which in turn contain table data tag, which contains the div with some text that I know beforehand. I want to be able to do something like this. Not sure what is the syntax for it.

String xpathofParentDiv=Parent div which contains &quot;//div[contains(text(),&#39;Sam&#39;)] and //div[contains(text(),&#39;Elliot&#39;)] and //div[contains(text(),&#39;Smith&#39;)]&quot;;

How do i go about locating the parent div, based on the text in the children divs located inside the table data <td>

答案1

得分: 1

使用以下 xpath

//div[.//div[text()='Sam'] 和 .//div[text()='Elliot'] 和 .//div[text()='Smith']]
英文:

Use the following xpath

//div[.//div[text()=&#39;Sam&#39;] and .//div[text()=&#39;Elliot&#39;] and .//div[text()=&#39;Smith&#39;]]

答案2

得分: 0

Parent是一个用于获取当前节点的父节点的方法。

我希望这是你要找的内容-

Xpath= //*[text()='Sam']//parent::div

英文:

Parent is a method used to retrieve the parent node of the current node

I hope this is what you lookin for-

Xpath= //*[text()='Sam']//parent::div

huangapple
  • 本文由 发表于 2020年10月14日 22:30:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64355553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定