Selenium Java: I want to access first div element inside a div class
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;
Using Xpath:
String btn=element.findElement(By.xpath("(//div[@class='recommendation-header-social-container'])[1]")).getAttribute("innerHTML");
Or use css selector
driver.findElement(By.cssSelector("div.recommendation-header-social-container > div:nth-child(1)"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With