Not able to get element by xpath inside div with ::before

假装没事ソ 提交于 2021-02-11 02:11:14

问题


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"]"}


回答1:


Presence of pseudo-element 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']")));
    



回答2:


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




回答3:


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());
                }


来源:https://stackoverflow.com/questions/60360940/not-able-to-get-element-by-xpath-inside-div-with-before

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!