Selenium和Java:查找没有良好id或选择器的嵌套元素

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

Selenium and Java: Finding nested elements without good id-s or selectors

问题

我在寻找页面中<li>元素的文本内容时遇到了一些问题,可悲的是,为Angular应用程序创建通用组件的小王们的层次结构几乎不可能为标记强加智能选择器。

以下是问题中的列表,包含来自<body>的所有标签:

&lt;app-root.....
 &lt;app-soknad.....
  &lt;app-oppsymmeriing-side
   &lt;main class=&quot;hb-side-hovedomrade&quot;&gt;
    &lt;section class=&quot;hb-seksjon&quot;&gt;
     &lt;app-oppsummering-personer-i-boligen&gt;
      &lt;div data-e2e-selector=&quot;personerCard&quot; class=&quot;hb-card&quot;&gt;
       &lt;div class=&quot;hb-card-topp&quot;&gt;&lt;h3 translate=&quot;&quot; class=&quot;hb-card-tittel&quot;&gt;在住人员&lt;/h3&gt;&lt;/div&gt;
        &lt;div class=&quot;hb-card-body&quot;&gt;
         &lt;ul class=&quot;hb-liste hb-liste--minimal&quot;&gt;
          &lt;li&gt;&lt;span aria-hidden=&quot;false&quot; class=&quot;hb-ikon hb-ikon--storrelse250&quot;&gt;&lt;/span&gt;E2efornavn E2eetternavn &lt;/li&gt;
          &lt;li&gt;&lt;span aria-hidden=&quot;false&quot; class=&quot;hb-ikon hb-ikon--storrelse250&quot;&gt;&lt;/span&gt;男孩,9 岁 &lt;/li&gt;
         &lt;/ul&gt;
        &lt;/div&gt;

Java 代码:

 List&lt;WebElement&gt; we = driver.findElements(By.cssSelector(&quot;[data-e2e-selector=personerCard]&gt;div&gt;div&gt;li&quot;));

List&lt;String&gt; beboere = driver.findElements(By.cssSelector(&quot;[data-e2e-selector=personerCard]&gt;div&gt;div&gt;li&quot;)).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>:

&lt;app-root....
 &lt;app-soknad....
  &lt;app-oppsymmeriing-side
   &lt;main class=&quot;hb-side-hovedomrade&quot;&gt;
    &lt;section class=&quot;hb-seksjon&quot;&gt;
     &lt;app-oppsummering-personer-i-boligen&gt;
      &lt;div data-e2e-selector=&quot;personerCard&quot; class=&quot;hb-card&quot;&gt;
       &lt;div class=&quot;hb-card-topp&quot;&gt;&lt;h3 translate=&quot;&quot; class=&quot;hb-card-tittel&quot;&gt;Personer i boligen&lt;/h3&gt;&lt;/div&gt;
        &lt;div class=&quot;hb-card-body&quot;&gt;
         &lt;ul class=&quot;hb-liste hb-liste--minimal&quot;&gt;
          &lt;li&gt;&lt;span aria-hidden=&quot;false&quot; class=&quot;hb-ikon hb-ikon--storrelse250&quot;&gt;&lt;/span&gt;E2efornavn E2eetternavn &lt;/li&gt;
          &lt;li&gt;&lt;span aria-hidden=&quot;false&quot; class=&quot;hb-ikon hb-ikon--storrelse250&quot;&gt;&lt;/span&gt;Gutt, 9 &#229;r &lt;/li&gt;
         &lt;/ul&gt;
        &lt;/div&gt;

Java code:

 List&lt;WebElement&gt; we = driver.findElements(By.cssSelector(&quot;[data-e2e-selector=personerCard]&gt;div&gt;div&gt;li&quot;));

List&lt;String&gt; beboere = driver.findElements(By.cssSelector(&quot;[data-e2e-selector=personerCard]&gt;div&gt;div&gt;li&quot;)).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 &gt; 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=&#39;personerCard&#39;] li

OR

[data-e2e-selector=&#39;personerCard&#39;] li

OR

[data-e2e-selector=&#39;personerCard&#39;] ul&gt;li

OR

 div[data-e2e-selector=&#39;personerCard&#39;] ul&gt;li

Therefore your java code should be

List&lt;WebElement&gt; we = driver.findElements(By.cssSelector(&quot;div[data-e2e-selector=&#39;personerCard&#39;] li&quot;));

OR

List&lt;WebElement&gt; we = driver.findElements(By.cssSelector(&quot;[data-e2e-selector=&#39;personerCard&#39;] li&quot;));

答案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&lt;String&gt; beboere = driver.findElements(By.cssSelector(&quot;[data-e2e-selector=personerCard] li&quot;)).stream().map(WebElement::getText).collect(Collectors.toList());

for(String text: beboere) {
	System.out.println(text);
}

huangapple
  • 本文由 发表于 2020年8月24日 23:16:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63563867.html
匿名

发表评论

匿名网友

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

确定