如何获取 <li>
中的文本使用 Selenium WebDriver (Java) 使用类名,而不是使用 xpath 或 css 定位器。
<body><table width="599" align="center" border="0" cellSpacing="0"
cellPadding="0"><tbody>
<tr>
<td width="599" class="error">
<font color="#ff0000">
<span class="errorHdr">The following errors occurred:</span>
<p></p><ul><ul><li>Invalid or incorrect user name entered.</li>
<li>Incorrect password entered. Passwords are case-sensitive. Please try again.</li>
</ul></ul>
</font>
</td>
</tr>
</tbody></table></body></html>
最佳答案
不,您无法使用类名获取 li 文本。使用 xpath 或 cssSelector 时遇到什么问题?这些是为像您这样的情况而设计的。无论如何,为了满足您的突发奇想,还有另一种方法可以做到这一点,即使用标记名,如下所述:
List<WebElement> allLi = driver.findElements(By.tagName("li"));
for(WebElement eachLi:allLi){
String tmp = eachLi.getText();
System.out.println("Text in li: "+tmp); //You may match for your required text, before printing/using it.
}
关于java - Selenium Webdriver 代码在类下查找 <li>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21631273/