我需要使用 Web 驱动程序对象获取 Web 元素列表
findElements(By.xpath(""));
我通过使用 xpath 作为 //*[@class=\"providers-list clearfix\"]
来获取列表但是,每当我尝试在
<div class="providers-list clearfix">::before
<div class="data-container">..</div>
</div>
这个 xpath 给我错误:
//[@class=\"data-container\"]" as no such element: Unable to locate element: {"method":"xpath","selector":"//[@class="data-container"]"}
最佳答案
存在 pseudo-element,例如::before 意味着该元素是:
- 样式或
- 插入到某些内容之前
因此,该元素本质上是动态的,要识别该元素,您必须引发 WebDriverWait 以获得所需的 visibilityOfElementLocated() 并且您可以使用以下任一 Locator Strategies :
css选择器
: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']")));
关于java - 无法使用::before 通过 div 内的 xpath 获取元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60360940/