如何使用Selenium和Java提取表元素的id属性的动态值 [英] How to extract the dynamic values of the id attributes of the table elements using Selenium and Java

查看:94
本文介绍了如何使用Selenium和Java提取表元素的id属性的动态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,其中的每一行都会有一个带有(部分)自动生成的id元素的下载链接.原因是实际的href元素始终为#",因此ID分开了下载.

I have a table where each row will have a download link with a (partly) auto-generated id element. The reason for this is that the actual href-element will allways be "#", so the id's separate the downloads.

我需要在td中找到该id元素的名称.那就是:我知道表行中有一个id元素,并且我知道该名称的一部分,并且我需要获取确切的名称.

I need to find the name of that id element in the td. That is: I know the table row has an id element, and I know part of the name, and I need to get the exact name.

我一次访问每一行,因此我一次只需要查看一次即可.无需浏览整个表格.

I'm accessing each row one at a time, so I only need to look within ONE td at a time. No need to look through the whole table.

当我知道名字时,我知道如何找到一个元素.但是当我只知道类型是另一回事时要找到元素.

I know well how to find an element when I know the name. But to find the element when I only know the type is another thing.

...
<tr>
 <td class="journalTable-journalPost"
  <a class="htext-small" href="#" id="downloadJournalPost-345">Download</a>
 </td>
</tr>
<tr>
 <td class="journalTable-journalPost"
  <a class="htext-small" href="#" id="downloadJournalPost-346">Download</a>
 </td>
</tr>
...

我无法在网络驱动程序中找到任何使我可以按类型查找元素的方法.

I cannot find any method(s) in the webdriver that lets me find an element by type.

可以使用部分名称,因为ID的名称为"downloadJournalPost-xxx",其中仅xxx更改.但是链接文本是唯一可以找到的值,它使我可以搜索部分匹配项.

Partial name would work, since the id's get the name "downloadJournalPost-xxx", where only xxx changes. But link text is the only value I can find which lets me search for a partial match.

更完整的标记.

<td class="journalTable-journalpost">
 <span class="hb-tekst--sekundar">In <!----><est-ikon class="ng-star-inserted">
  <div aria-hidden="true" class="hb-ikon hb-ikon--pil3-inn  ">
   <svg focusable="false">
    <use xlink:href="#ikon-pil3-inn"></use>
   </svg>
  </div></est-ikon><!----></span>
 <span class="hb-tekst--mellomTittel hb-avstandIngen"> Application and attachments</span>
 <a class="hb-tekst--liten" href="#" id="lastNedJournalPost-2892">Download journal post</a>
</td>

推荐答案

要打印所需元素的 id 属性列表visibilityOfAllElementsLocatedBy()诱导 WebDriverWait ,您可以使用 Java8 定位器策略:

To print the List of id attribute of the element you need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use Java8 stream() and map() and you can use either of the following Locator Strategies:

  • cssSelector:

List<String> myID = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("td.journalTable-journalPost>a.htext-small"))).stream().map(element->element.getAttribute("id")).collect(Collectors.toList());
System.out.println(myIDs);

  • xpath:

    List<String> myIDs = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//td[@class='journalTable-journalPost']/a[@class='htext-small' and text()='Download']"))).stream().map(element->element.getAttribute("id")).collect(Collectors.toList());
    System.out.println(myIDs);
    

  • 这篇关于如何使用Selenium和Java提取表元素的id属性的动态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆