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

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

Java and Selenium: Getting the id of list elements

问题

  1. 我有一个相当简单的列表结构,其中元素具有ID和自定义选择器。
  2. 在获取ID(和其他)选择器的值方面我遇到了一些困难。我可以获取文本内容,但是相同的方法似乎不能用于获取选择器的值。
  3. <ul data-e2e-selector="checkboxliste" class="hb-feltliste">
  4. <li>
  5. <input type="checkbox" class="hb-checkbox" id="tilpasninger-INNGANG" data-e2e-selector="tilpasninger-INNGANG">
  6. <label class="hb-label" for="tilpasninger-INNGANG">Inngangsparti </label>
  7. </li>
  8. <li>
  9. <input type="checkbox" class="hb-checkbox" id="tilpasninger-ETT_PLAN" data-e2e-selector="tilpasninger-ETT_PLAN">
  10. <label class="hb-label" for="tilpasninger-ETT_PLAN">Alle n&#248;dvendige rom p&#229; ett plan</label>
  11. </li>
  12. </ul>
  1. 这段代码获取文本内容
  2. return driver.findElements(By.cssSelector("[data-e2e-selector=checkboxliste] li"))
  3. .stream().map(WebElement::getText).collect(Collectors.toList());
  4. 但是以下代码无法获取ID
  5. List<WebElement> liste = driver.findElements(By.cssSelector("[data-e2e-selector=checkboxliste] li"));
  6. String valg = liste.get(0).getAttribute("id").toString();
  7. 我必须承认我不擅长使用流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.

  1. &lt;ul data-e2e-selector=&quot;checkboxliste&quot; class=&quot;hb-feltliste&quot;&gt;
  2. &lt;li&gt;
  3. &lt;input type=&quot;checkbox&quot; class=&quot;hb-checkbox id=&quot;tilpasninger-INNGANG&quot; data-e2e-selector=&quot;tilpasninger-INNGANG&quot;&gt;
  4. &lt;label class=&quot;hb-label&quot; for=&quot;tilpasninger-INNGANG&quot;&gt;Inngangsparti &lt;/label&gt;
  5. &lt;/li&gt;
  6. &lt;li&gt;
  7. &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;
  8. &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;
  9. &lt;/li&gt;
  10. &lt;/ul&gt;

This code gets the text contents.

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

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

  1. List&lt;WebElement&gt; liste = driver.findElements(By.cssSelector(&quot;[data-e2e-selector=checkboxliste] li&quot;));
  2. 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 元素。
要选择你想要的内容,你需要类似这样的代码:

  1. 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 代码中存在错误:

  1. <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:

  1. 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:

  1. &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:

确定