我在计算以下HTML中的元素时遇到问题:
<div id="photos-view" class="all visible">
<div class="sort-container">
<h2 class="sort-title sort-zone">Zone1<span>3</span></h2>
<section data-index="0" class="section-0">
<div id="photo-holder-9C9E72AA-5A40-D1A1-BF44-CB5ED13EA628" class="photo-holder" data-id="9C9E72AA-5A40-D1A1-BF44-CB5ED13EA628" data-itemid="9C9E72AA-5A40-D1A1-BF44-CB5ED13EA628">
<picture id="photo-9C9E72AA-5A40-D1A1-BF44-CB5ED13EA628">
<img srcset="" src="data:image/jpeg;base64" data-sheet-id="32" data-itemid="9C9E72AA-5A40-D1A1-BF44-CB5ED13EA628" data-id="9C9E72AA-5A40-D1A1-BF44-CB5ED13EA628" data-index="0" data-group="0283">
</picture>
</div>
<div id="photo-holder-667CBB46-6BC4-3BCE-69F6-DB79482204F6" class="photo-holder" data-id="667CBB46-6BC4-3BCE-69F6-DB79482204F6" data-itemid="667CBB46-6BC4-3BCE-69F6-DB79482204F6">
<picture id="photo-667CBB46-6BC4-3BCE-69F6-DB79482204F6">
<img srcset="" src="data:image/jpeg;base64" data-sheet-id="32" data-itemid="667CBB46-6BC4-3BCE-69F6-DB79482204F6" data-id="667CBB46-6BC4-3BCE-69F6-DB79482204F6" data-index="1" data-group="0283">
</picture>
</div>
</section>
</div>
<div class="sort-container">
<h2 class="sort-title sort-zone">Zone2<span>1</span></h2>
<section data-index="1" class="section-1">
<div id="photo-holder-C1131DD2-B422-FE8E-2AF0-2581527CEA9E" class="photo-holder" data-id="C1131DD2-B422-FE8E-2AF0-2581527CEA9E" data-itemid="C1131DD2-B422-FE8E-2AF0-2581527CEA9E">
<picture id="photo-C1131DD2-B422-FE8E-2AF0-2581527CEA9E">
<img srcset="" src="data:image/jpeg;base64" data-sheet-id="34" data-itemid="C1131DD2-B422-FE8E-2AF0-2581527CEA9E" data-id="C1131DD2-B422-FE8E-2AF0-2581527CEA9E" data-index="0" data-group="2831">
</picture>
</div>
</section>
</div>
</div>
正如您所看到的,有两个名为“sort-title sort-zone”的类。我需要检查我的测试:
答案 0 :(得分:1)
您可以将WebElements简单地保存在类sort-container
的列表中,如下所示:
List<WebElement> list = driver.findElements(By.className("sort-container"));
并使用:
list.get(0).getText();
list.get(1).getText();
可以给你:
Zone13
Zone21
作为可用于断言的输出。
答案 1 :(得分:0)
我建议使用JSoup来解析文档。
Document doc = Jsoup.parse(html);
List<String> L = doc.select('.sort-title.sort-zone').eachText();
return L.size()==2 && L.get(0).equals("Zone13") && L.get(1).equals("Zone21");
这是解决您的问题的示例代码。随意根据您的需求进行改进。