英文:
Selenium and Java: Finding nested elements without good id-s or selectors
问题
我在寻找页面中<li>元素的文本内容时遇到了一些问题,可悲的是,为Angular应用程序创建通用组件的小王们的层次结构几乎不可能为标记强加智能选择器。
以下是问题中的列表,包含来自<body>的所有标签:
<app-root.....
<app-soknad.....
<app-oppsymmeriing-side
<main class="hb-side-hovedomrade">
<section class="hb-seksjon">
<app-oppsummering-personer-i-boligen>
<div data-e2e-selector="personerCard" class="hb-card">
<div class="hb-card-topp"><h3 translate="" class="hb-card-tittel">在住人员</h3></div>
<div class="hb-card-body">
<ul class="hb-liste hb-liste--minimal">
<li><span aria-hidden="false" class="hb-ikon hb-ikon--storrelse250"></span>E2efornavn E2eetternavn </li>
<li><span aria-hidden="false" class="hb-ikon hb-ikon--storrelse250"></span>男孩,9 岁 </li>
</ul>
</div>
Java 代码:
List<WebElement> we = driver.findElements(By.cssSelector("[data-e2e-selector=personerCard]>div>div>li"));
List<String> beboere = driver.findElements(By.cssSelector("[data-e2e-selector=personerCard]>div>div>li")).stream().map(WebElement::getText).collect(Collectors.toList()));
以上代码无法获取任何结果。我不应该指定<li>之前的所有标签,对吗?在<ul>上放置选择器是不可能的,尽管那可能是最好的解决方案。或许可以使用带有 data-e2e-selector 属性的<div> 元素吗?
英文:
I'm having some trouble finding the text contents of <li> elements in a page, where - sadly - the hierarchy of the small kings creating common components for the Angular applications makes it all but impossible to impose intelligent selectors for the markup.
This is the list in question, with all the tags from <body>:
<app-root....
<app-soknad....
<app-oppsymmeriing-side
<main class="hb-side-hovedomrade">
<section class="hb-seksjon">
<app-oppsummering-personer-i-boligen>
<div data-e2e-selector="personerCard" class="hb-card">
<div class="hb-card-topp"><h3 translate="" class="hb-card-tittel">Personer i boligen</h3></div>
<div class="hb-card-body">
<ul class="hb-liste hb-liste--minimal">
<li><span aria-hidden="false" class="hb-ikon hb-ikon--storrelse250"></span>E2efornavn E2eetternavn </li>
<li><span aria-hidden="false" class="hb-ikon hb-ikon--storrelse250"></span>Gutt, 9 år </li>
</ul>
</div>
Java code:
List<WebElement> we = driver.findElements(By.cssSelector("[data-e2e-selector=personerCard]>div>div>li"));
List<String> beboere = driver.findElements(By.cssSelector("[data-e2e-selector=personerCard]>div>div>li")).stream().map(WebElement::getText).collect(Collectors.toList());
I'm unable to get any results with the above code. I shouldn't have to specify all the tags preceding the <li>, should I? It's not possible to put a selector on the <ul>, which would have been the best solution. Is it perhaps possible to use the <div> with the data-e2e-selector?
答案1
得分: 1
使用选择器
.hb-liste.hb-liste--minimal > li
https://seleniumbyexamples.github.io/cssselectorclassmultiple
英文:
use selector
.hb-liste.hb-liste--minimal > li
https://seleniumbyexamples.github.io/cssselectorclassmultiple
答案2
得分: 1
获取带有data-e2e-selector
属性引用的li
元素列表,请使用以下CSS选择器。
div[data-e2e-selector='personerCard'] li
或者
[data-e2e-selector='personerCard'] li
或者
[data-e2e-selector='personerCard'] ul>li
或者
div[data-e2e-selector='personerCard'] ul>li
因此,您的Java代码应为
List<WebElement> we = driver.findElements(By.cssSelector("div[data-e2e-selector='personerCard'] li"));
或者
List<WebElement> we = driver.findElements(By.cssSelector("[data-e2e-selector='personerCard'] li"));
英文:
To get the list of li
elements with reference to data-e2e-selector
use following css selector.
div[data-e2e-selector='personerCard'] li
OR
[data-e2e-selector='personerCard'] li
OR
[data-e2e-selector='personerCard'] ul>li
OR
div[data-e2e-selector='personerCard'] ul>li
Therefore your java code should be
List<WebElement> we = driver.findElements(By.cssSelector("div[data-e2e-selector='personerCard'] li"));
OR
List<WebElement> we = driver.findElements(By.cssSelector("[data-e2e-selector='personerCard'] li"));
答案3
得分: 0
你可以使用这个选择器 [data-e2e-selector=personerCard] li
:
List<String> beboere = driver.findElements(By.cssSelector("[data-e2e-selector=personerCard] li")).stream().map(WebElement::getText).collect(Collectors.toList());
for (String text : beboere) {
System.out.println(text);
}
英文:
You can use this selector [data-e2e-selector=personerCard] li
:
List<String> beboere = driver.findElements(By.cssSelector("[data-e2e-selector=personerCard] li")).stream().map(WebElement::getText).collect(Collectors.toList());
for(String text: beboere) {
System.out.println(text);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论