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

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

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.

&lt;div class=&quot;generic classname&quot; id=&quot;generic ID name&quot; &gt;  // div1
&lt;div class=&quot;presentation&quot; id=&quot;body presentation&quot;&gt;      // div2
&lt;font&gt;unique text&lt;/font&gt;
&lt;div class= &quot;generic classname&quot; id=&quot;generic ID name&quot;&quot;&gt; //div3
// under this div I have the table entry.
// multiple &lt;td&gt; and &lt;tr&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

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()=&#39;unique text&#39;]]/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 轴向向上。

在开发工具中如下所示:
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">
	<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()=&#39;unique text&#39;]/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()=&#39;unique text&#39;]/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".

Looks like this in devtools:
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

This is based on your html looking like:

&lt;div class=&quot;generic classname&quot; id=&quot;generic ID name&quot; &gt;
	&lt;div class=&quot;presentation&quot; id=&quot;body presentation&quot;&gt;
		&lt;font&gt;unique text&lt;/font&gt;
		&lt;div class=&quot;generic classname&quot; id=&quot;generic ID name&quot;&quot;&gt; 
            // under this div I have the table entry.
            // multiple &lt;td&gt; and &lt;tr&gt;
		&lt;/div&gt;
	&lt;/div&gt;
&lt;/div&gt;

Different HTML would need a different xpath so please say if you need to update.

答案3

得分: 1

根据HTML:

&lt;div class=&quot;generic classname&quot; id=&quot;generic ID name&quot; &gt;  // div1
	&lt;div class=&quot;presentation&quot; id=&quot;body presentation&quot;&gt;      // div2
		&lt;font&gt;unique text&lt;/font&gt;
		&lt;div class= &quot;generic classname&quot; id=&quot;generic ID name&quot;&gt; //div3
			// 在此div下有表格条目。
			// 多个 &lt;td&gt; 和 &lt;tr&gt;
		&lt;/div&gt;
	&lt;/div&gt;
&lt;/div&gt;

由于您的用例不依赖于任何&lt;div1&gt;属性,您可以轻松地不考虑&lt;div1&gt;


解决方案

要定位第三个&lt;div&gt;,您有以下四种方法:

  • 使用文本 unique text&lt;div&gt; 属性:

    //font[text()=&#39;unique text&#39;]//following::div[@class=&#39;generic classname&#39; and @id=&#39;generic ID name&#39;]
    
  • 使用文本 unique textindex

    //font[text()=&#39;unique text&#39;]//following::div[1]
    
  • 使用具有子元素 &lt;font&gt;,其文本为 unique text&lt;div&gt; 属性的 &lt;div2&gt;

    //div[./font[text()=&#39;unique text&#39;]]//following-sibling::div[@class=&#39;generic classname&#39; and @id=&#39;generic ID name&#39;]
    
  • 使用具有子元素 &lt;font&gt;,其文本为 unique textindex&lt;div2&gt;

    //div[./font[text()=&#39;unique text&#39;]]//following-sibling::div[1]
    
英文:

As per the HTML:

&lt;div class=&quot;generic classname&quot; id=&quot;generic ID name&quot; &gt;  // div1
	&lt;div class=&quot;presentation&quot; id=&quot;body presentation&quot;&gt;      // div2
		&lt;font&gt;unique text&lt;/font&gt;
		&lt;div class= &quot;generic classname&quot; id=&quot;generic ID name&quot;&gt; //div3
			// under this div I have the table entry.
			// multiple &lt;td&gt; and &lt;tr&gt;
		&lt;/div&gt;
	&lt;/div&gt;
&lt;/div&gt;

As your usecase is not dependent on any of the &lt;div1&gt; attributes, you can easily avoid considering &lt;div1&gt;.


Solution

To locate the third &lt;div&gt; you have four approaches as follows:

  • Using the text unique text and &lt;div&gt; attributes:

    //font[text()=&#39;unique text&#39;]//following::div[@class=&#39;generic classname&#39; and @id=&#39;generic ID name&#39;]
    
  • Using the textunique text and index:

    //font[text()=&#39;unique text&#39;]//following::div[1]
    
  • Using the &lt;div2&gt; which have a child &lt;font&gt; tag with text as unique text and &lt;div&gt; attributes:

    //div[./font[text()=&#39;unique text&#39;]]//following-sibling::div[@class=&#39;generic classname&#39; and @id=&#39;generic ID name&#39;]
    
  • Using the &lt;div2&gt; which have a child &lt;font&gt; tag with text as unique text and index:

    //div[./font[text()=&#39;unique text&#39;]]//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.

&lt;script&gt;
  // get the element
  let elem = document.getElementsByName(&#39;font&#39;);
&lt;/script&gt;

https://www.w3schools.com/jsref/met_doc_getelementsbyname.asp

答案5

得分: -1

一个更多的选项供您选择:

//div[font='独特文本']/div
英文:

One more option for you:

//div[font=&#39;unique text&#39;]/div

huangapple
  • 本文由 发表于 2020年8月20日 20:44:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63505400.html
匿名

发表评论

匿名网友

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

确定