简体   繁体   English

如何从中获取第二个ID<div> 如果它有多个具有相同类名和多个 div 标签的类,则使用 xpath 标签</div><div id="text_translate"><p>观察到唯一的唯一值是 id,在这种情况下我们如何获取。</p><p> 请查找HTML:</p><pre> <div id="0007" data-activity-type="CompatCheck" class="Activity"></div> <div id="110007" data-activity-type="CompatCheck" class="Activity"</div></pre><p> 尝试使用以下代码行时:</p><pre> findElement(By.xpath("//div[@data-activity-type='CompatCheck']")).getAttribute("id");</pre><p> 我只得到第一个ID,即; 0007</p><p> 但我总是需要第二个 id="110007",你能建议我获取第二个 id</p><p> 预计 output:110007</p></div>

[英]How to fetch second id from <div> tag using xpath if its has multiple classes with same classname and multiple div tag

Observed that the only unique value is id, how can we fetch in this case.观察到唯一的唯一值是 id,在这种情况下我们如何获取。

Please Find the HTML:请查找HTML:

<div id="0007" data-activity-type="CompatCheck" class="Activity"></div>
                                
<div id="110007" data-activity-type="CompatCheck" class="Activity"</div>

While trying to use following code line:尝试使用以下代码行时:

findElement(By.xpath("//div[@data-activity-type='CompatCheck']")).getAttribute("id");

I'm getting only first id ie;我只得到第一个ID,即; 0007 0007

but I need always the second id="110007", can you please suggest to get the second id但我总是需要第二个 id="110007",你能建议我获取第二个 id

Expected output: 110007预计 output:110007

In case you always need the second element id you can update your XPath accordingly.如果您始终需要第二个元素 ID,您可以相应地更新您的 XPath。
This should work:这应该工作:

findElement(By.xpath("(//div[@data-activity-type='CompatCheck'])[2]")).getAttribute("id");

如何从中获取ID<div> 如果它有多个具有相同类名的类,则使用 xpath 标记</div><div id="text_translate"><p>请找HTML:</p><pre> <div id="086" data-activity-type="CompatCheck" class="Activity"></div></pre><p> 这里只有常量值是data-activity-type="CompatCheck"并且类名对于所有其他元素都是相同的。</p><p> 在尝试使用data-activity-type <em>CompatCheck</em></p><p> 预计 output:</p><pre> 086</pre><p> 问题2:观察到唯一唯一值是id,在这种情况下我们如何获取。</p><p> 请查找HTML:</p><pre> <div id="0007" data-activity-type="CompatCheck" class="Activity"></div> <div id="110007" data-activity-type="CompatCheck" class="Activity"</div></pre><p> 尝试使用以下代码行时:</p><pre> findElement(By.xpath("//div[@data-activity-type='CompatCheck']")).getAttribute("id");</pre><p> 我只得到第一个ID,即; 0007</p><p> 但我总是需要第二个 id="110007",你能建议我获取第二个 id</p></div> - How to fetch id from <div> tag using xpath if its has multiple classes with same classname

如何从中获取ID<div> 以有效的方式使用 xpath 标记</div><div id="text_translate"><p>尝试了很多方法来获取 ID,但我无法找到确切的 ID 或有效的结果,因为有多个 ID 并且它共享多个相同的 class。</p><p> 尝试了以下方法,但我没有得到所需的确切 ID</p><pre> //Approach 1 divId = driver.findElement(By.xpath("(//div[@data-activity-type='CompatCheck'])")).getAttribute("id"); //Approach 2 divId = driver.findElement(By.xpath("(//div[@data-activity-type='CompatCheck'])[2]")).getAttribute("id"); //Approach 3 driver.findElement(By.xpath("//div[@data-activity-type='CompatCheck'] and contains(.,'AndroidTesting')]")).getAttribute("id"));</pre><p> 请在下面找到HTML代码:</p><pre> <div> <strong>Reena</strong>: <label id="compatcheck_label"> <em class="Highlight" style="padding: 1px; box-shadow: rgb(229, 229, 229) 1px 1px; border-radius: 3px; background-color: rgb(0, 191, 255); color: rgb(0, 0, 0); font-style: inherit;" match="test" loopnumber="472046515">Test</em>device testing - AndroidTesting </label> </div> <div> <span style="float: none;" class="special-apps-list">(Special Tag) </span> <label id="label_1100"></label> <span style="float: none;">0d 4h 39m 49s</span> </div> <div id="div_1100" data-activity-type="CompatCheck" class="mainActivity"> </div></pre><p> 从上面的HTML代码观察到label id和div id是一样的(1100),和其他class不同的是“device testing - AndroidTesting”</p><p> 有没有什么方法可以通过使用“设备测试 - AndroidTesting”获得 label id 或 div id(因为两者相同),因为它是唯一且不变的元素。</p><p> 预期 output 使用“设备测试 - AndroidTesting”作为唯一</p><pre>label_1100 div_1100</pre></div> - How to fetch id from <div> tag using xpath in efficient way

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从中获取ID<div> 如果它有多个具有相同类名的类,则使用 xpath 标记</div><div id="text_translate"><p>请找HTML:</p><pre> <div id="086" data-activity-type="CompatCheck" class="Activity"></div></pre><p> 这里只有常量值是data-activity-type="CompatCheck"并且类名对于所有其他元素都是相同的。</p><p> 在尝试使用data-activity-type <em>CompatCheck</em></p><p> 预计 output:</p><pre> 086</pre><p> 问题2:观察到唯一唯一值是id,在这种情况下我们如何获取。</p><p> 请查找HTML:</p><pre> <div id="0007" data-activity-type="CompatCheck" class="Activity"></div> <div id="110007" data-activity-type="CompatCheck" class="Activity"</div></pre><p> 尝试使用以下代码行时:</p><pre> findElement(By.xpath("//div[@data-activity-type='CompatCheck']")).getAttribute("id");</pre><p> 我只得到第一个ID,即; 0007</p><p> 但我总是需要第二个 id="110007",你能建议我获取第二个 id</p></div> - How to fetch id from <div> tag using xpath if its has multiple classes with same classname 如何从中获取ID<div> 以有效的方式使用 xpath 标记</div><div id="text_translate"><p>尝试了很多方法来获取 ID,但我无法找到确切的 ID 或有效的结果,因为有多个 ID 并且它共享多个相同的 class。</p><p> 尝试了以下方法,但我没有得到所需的确切 ID</p><pre> //Approach 1 divId = driver.findElement(By.xpath("(//div[@data-activity-type='CompatCheck'])")).getAttribute("id"); //Approach 2 divId = driver.findElement(By.xpath("(//div[@data-activity-type='CompatCheck'])[2]")).getAttribute("id"); //Approach 3 driver.findElement(By.xpath("//div[@data-activity-type='CompatCheck'] and contains(.,'AndroidTesting')]")).getAttribute("id"));</pre><p> 请在下面找到HTML代码:</p><pre> <div> <strong>Reena</strong>: <label id="compatcheck_label"> <em class="Highlight" style="padding: 1px; box-shadow: rgb(229, 229, 229) 1px 1px; border-radius: 3px; background-color: rgb(0, 191, 255); color: rgb(0, 0, 0); font-style: inherit;" match="test" loopnumber="472046515">Test</em>device testing - AndroidTesting </label> </div> <div> <span style="float: none;" class="special-apps-list">(Special Tag) </span> <label id="label_1100"></label> <span style="float: none;">0d 4h 39m 49s</span> </div> <div id="div_1100" data-activity-type="CompatCheck" class="mainActivity"> </div></pre><p> 从上面的HTML代码观察到label id和div id是一样的(1100),和其他class不同的是“device testing - AndroidTesting”</p><p> 有没有什么方法可以通过使用“设备测试 - AndroidTesting”获得 label id 或 div id(因为两者相同),因为它是唯一且不变的元素。</p><p> 预期 output 使用“设备测试 - AndroidTesting”作为唯一</p><pre>label_1100 div_1100</pre></div> - How to fetch id from <div> tag using xpath in efficient way 如果共享相同的类名,如何使用 xpath 或 cssSelector 获取多个 ID - How to fetch multiple ID using xpath or cssSelector if its sharing the same classname 如何为 div 标签的多个兄弟编写 XPath - How to write XPath for multiple sibling of div tag xpath 具有多个条件,其中将包含“a”和“div”标签 - xpath with multiple conditions which will contain 'a' and 'div' tag 如何在div标签中获取Xpath? - How to get Xpath in div tag? 标识属性中具有span和div的Tag的XPath - Identifying an XPath for a Tag which an attribute has span and div in it 如何处理带有 div 标签和多个属性的下拉列表? - How to handle drop down list with div tag and multiple attributes? 如何从具有带有已知文本的标题标签的Div中获取Table元素-使用Selenium和Java - how to get Table element from a Div that has a Heading tag with a known text - using selenium and java 如何使用Xpath Selenium WebDriver JAVA获取多个元素(div)? - How to get multiple elements (div) using Xpath Selenium WebDriver JAVA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM