Selenium Java:我想访问 div 类中的第一个 div 元素
public int checkLinks()
{
int x = driver.findElements(By.xpath("//div[@class='recommendation-header-social-container']/div")).size();
List<WebElement> y = driver.findElements(By.xpath("//div[@class='recommendation-header-social-container']/div"));
int i=0;
for(WebElement element:y)
{
String btn=element.findElement(By.xpath("//div[@class='recommendation-header-social-container']")).getAttribute("innerHTML");
System.out.println("Length of first element: "+btn.length());
}
return x;
使用 Xpath:
String btn=element.findElement(By.xpath("(//div[@class='recommendation-header-social-container'])[1]")).getAttribute("innerHTML");
或者使用CSS选择器
driver.findElement(By.cssSelector("div.recommendation-header-social-container > div:nth-child(1)"));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)