英文:
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ødvendige rom på 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.
<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ødvendige rom på ett plan</label>
</li>
</ul>
This code gets the text contents.
return driver.findElements(By.cssSelector("[data-e2e-selector=checkboxliste] li"))
.stream().map(WebElement::getText).collect(Collectors.toList());
But this, for instance, does NOT get the ID values:
List<WebElement> liste = driver.findElements(By.cssSelector("[data-e2e-selector=checkboxliste] li"));
String valg = helvete.get(0).getAttribute("id").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
元素下的任何具有 id
或 data-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<WebElement> liste = driver.findElements(By.cssSelector("[data-e2e-selector=checkboxliste] li [id], [data-e2e-selector=checkboxliste] li [data-e2e-selector]"));
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:
<input type="checkbox" class="hb-checkbox id="tilpasninger-INNGANG" data-e2e-selector="tilpasninger-INNGANG">
Class attribute is missing the end quote.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论