简体   繁体   English

无法通过 ::before 在 div 内通过 xpath 获取元素

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

I need to get the list of web elements by using web driver object我需要使用 Web 驱动程序对象获取 Web 元素列表

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我通过使用 xpath 作为//*[@class=\\"providers-list clearfix\\"]来获取//*[@class=\\"providers-list clearfix\\"]但是,每当我尝试获取其中的元素时都会出现错误

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

This xpath gives me error:这个 xpath 给了我错误:

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

Presence of eg ::before implies that the element is either: 存在,例如::before意味着该元素是:

  • 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 :因此,元素本质上是动态的,为了识别元素,您必须诱导WebDriverWait以获得所需的visibilityOfElementLocated() ,您可以使用以下任一定位器策略

  • cssSelector : cssSelector

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

     WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='providers-list clearfix']//div[@class='data-container']")));

请使用此 X 路径 - //*[contains (@class, 'clearfix')]

are you trying to access from the object which did findElements?您是否试图从 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());
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM