Java和Selenium:获取列表元素的id

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

Java and Selenium: Getting the id of list elements

问题

我有一个相当简单的列表结构,其中元素具有ID和自定义选择器。

在获取ID(和其他)选择器的值方面我遇到了一些困难。我可以获取文本内容,但是相同的方法似乎不能用于获取选择器的值。

<ul data-e2e-selector="checkboxliste" class="hb-feltliste">
 <li>
  <input type="checkbox" class="hb-checkbox" id="tilpasninger-INNGANG" data-e2e-selector="tilpasninger-INNGANG">
  <label class="hb-label" for="tilpasninger-INNGANG">Inngangsparti </label>
 </li>
 <li>
  <input type="checkbox" class="hb-checkbox" id="tilpasninger-ETT_PLAN" data-e2e-selector="tilpasninger-ETT_PLAN">
  <label class="hb-label" for="tilpasninger-ETT_PLAN">Alle n&#248;dvendige rom p&#229; ett plan</label>
 </li>
</ul>
这段代码获取文本内容

return driver.findElements(By.cssSelector("[data-e2e-selector=checkboxliste] li"))
        .stream().map(WebElement::getText).collect(Collectors.toList());

但是以下代码无法获取ID值

List<WebElement> liste = driver.findElements(By.cssSelector("[data-e2e-selector=checkboxliste] li"));
String valg = liste.get(0).getAttribute("id").toString();

我必须承认我不擅长使用流streams),我正在尝试修改现有的代码来学习一些但是即使没有那个问题我似乎仍然无法获取选择器的值
英文:

I've got a pretty simple list structure, where the elements have an ID and a custom selector.

I'm struggling a bit getting the value of the ID (and other) selector. I can get the text contents, but the same approach doesn't seem to work for getting the selector values.

&lt;ul data-e2e-selector=&quot;checkboxliste&quot; class=&quot;hb-feltliste&quot;&gt;
 &lt;li&gt;
  &lt;input type=&quot;checkbox&quot; class=&quot;hb-checkbox id=&quot;tilpasninger-INNGANG&quot; data-e2e-selector=&quot;tilpasninger-INNGANG&quot;&gt;
  &lt;label class=&quot;hb-label&quot; for=&quot;tilpasninger-INNGANG&quot;&gt;Inngangsparti &lt;/label&gt;
 &lt;/li&gt;
 &lt;li&gt;
  &lt;input type=&quot;checkbox&quot; class=&quot;hb-checkbox id=&quot;tilpasninger-ETT_PLAN&quot; data-e2e-selector=&quot;tilpasninger-ETT_PLAN&quot;&gt;
  &lt;label class=&quot;hb-label&quot; for=&quot;tilpasninger-ETT_PLAN&quot;&gt;Alle n&#248;dvendige rom p&#229; ett plan&lt;/label&gt;
 &lt;/li&gt;
&lt;/ul&gt;

This code gets the text contents.

return driver.findElements(By.cssSelector(&quot;[data-e2e-selector=checkboxliste] li&quot;))
        .stream().map(WebElement::getText).collect(Collectors.toList());

But this, for instance, does NOT get the ID values:

List&lt;WebElement&gt; liste = driver.findElements(By.cssSelector(&quot;[data-e2e-selector=checkboxliste] li&quot;));
String valg = helvete.get(0).getAttribute(&quot;id&quot;).toString();

I must admit I'm not good with streams, and I'm trying to modify existing code to learn a bit. But even without that, I cannot seem to get the selector values.

答案1

得分: 0

你的 CSS 选择器是错误的,它只选择了 li 元素。
要选择你想要的内容,你需要类似这样的代码:

List<WebElement> liste = driver.findElements(By.cssSelector("[data-e2e-selector=checkboxliste] li [id], [data-e2e-selector=checkboxliste] li [data-e2e-selector]"));

这将选择在你的 li 元素下的任何具有 iddata-e2e-selector 属性的元素。

此外,你的 HTML 代码中存在错误:

<input type="checkbox" class="hb-checkbox id="tilpasninger-INNGANG" data-e2e-selector="tilpasninger-INNGANG">

class 属性缺少结束引号。

英文:

Your CSS selector is wrong, it only selects the li elements.
In order to select what you want you need something like:

List&lt;WebElement&gt; liste = driver.findElements(By.cssSelector(&quot;[data-e2e-selector=checkboxliste] li [id], [data-e2e-selector=checkboxliste] li [data-e2e-selector]&quot;));

This will select any element under your li which has either id or data-e2e-selector attribute.

Also, you have an error in your HTML:

&lt;input type=&quot;checkbox&quot; class=&quot;hb-checkbox id=&quot;tilpasninger-INNGANG&quot; data-e2e-selector=&quot;tilpasninger-INNGANG&quot;&gt;

Class attribute is missing the end quote.

huangapple
  • 本文由 发表于 2020年8月27日 17:45:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63613347.html
匿名

发表评论

匿名网友

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

确定