gpt4 book ai didi

selenium-webdriver - Selenium Java : I want to access first div element inside a div class

转载 作者:行者123 更新时间:2023-12-05 03:07:00 26 4
gpt4 key购买 nike

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)"));

关于selenium-webdriver - Selenium Java : I want to access first div element inside a div class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48591753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com