如何使用selenium li
打印所有webdriver
项目文字。
在这里,我想要某种类型的单行代码打印所有li
项文本,如
保护等。可以通过使用for循环
<div class="dropdown change_right">
<ul>
<li>
<a class="" title="Jackets" href="/collections/jackets">Jackets</a>
</li>
<li>
<a title="Jackets protective" href="/collections/jackets/jackets-protective" class="sub-link"> protective</a>
</li>
<li>
<a title="casual leather" href="/collections/jackets/casual-leather" class="sub-link">casual leather</a>
</li>
<li>
<a class="" title="T-shirts & shirts " href="/collections/t-shirts-shirts">T-shirts & shirts </a>
</li>
<li>
<a title="shirts" href="/collections/t-shirts-shirts/shirts" class="sub-link">shirts</a>
</li>
<li>
<a title="crew-neck" href="/collections/t-shirts-shirts/crew-neck" class="sub-link">crew-neck</a>
</li>
<li>
<a title="polo tshirts" href="/collections/t-shirts-shirts/polo-tshirts" class="sub-link">polo tshirts</a>
</li>
<li>
<a class="" title="Trousers" href="/collections/trousers">Trousers</a>
</li>
<li>
<a title="Trousers Protective" href="/collections/trousers/trousers-protective" class="sub-link"> Protective</a>
</li>
</ul>
答案 0 :(得分:0)
可以使用简单的findElements()
完成。在xpath
的帮助下,抓取elements
的集合并通过集合进行简单迭代,并在getText()
的帮助下打印值,返回元素的字符串值。
List<WebElement> allText = driver.findElements(By.xpath("//*[contains(@class,'dropdown')]//a"));
for ( WebElement element: allText) {
System.out.println(element.getText());
}
注意:未经测试的代码。您可能需要确保在执行此代码之前单击并打开列表,并且也可能需要一些等待