1

I need to get the list of web elements by using web driver object

findElements(By.xpath(""));

I get the list by using xpath as //*[@class=\"providers-list clearfix\"].However, I get an error whenever I try to fetch element inside

<div class="providers-list clearfix">::before 
  <div class="data-container">..</div>
</div>

This xpath gives me error:

//[@class=\"data-container\"]" as no such element: Unable to locate element: {"method":"xpath","selector":"//[@class="data-container"]"}

Sers
  • 12,047
  • 2
  • 12
  • 31
Vineet
  • 11
  • 1
  • 2

3 Answers3

1

Presence of e.g. ::before implies that the element is either:

  • Styled or
  • Inserted before some content

So the element is dynamic in nature and to identify the element you have to induce WebDriverWait for the desired visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • cssSelector:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.providers-list.clearfix div.data-container")));
    
  • xpath:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='providers-list clearfix']//div[@class='data-container']")));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

please use this X-path - //*[contains (@class, ‘clearfix')]

  • thanks, Abhishek it still gives me an error . Actually I need to fetch data from div inside class="data-container". – Vineet Feb 23 '20 at 13:17
0

are you trying to access from the object which did findElements? This is how it works ideally:

    List <WebElement> myList = driver.findElements(By.xpath("//*[@class=\"providers-list clearfix\"]"));
                for(WebElement eachList:myList)
                {
         System.out.println(eachList.findElement(By.xpath("div[1]")).getText());
                }
ashwinin
  • 81
  • 5