无法通过 ::before 在 div 内通过 xpath 获取元素 [英] Not able to get element by xpath inside div with ::before

查看:661
本文介绍了无法通过 ::before 在 div 内通过 xpath 获取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用网络驱动程序对象获取网络元素列表

findElements(By.xpath(""));

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

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

这个 xpath 给了我错误:

<块引用>

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

解决方案

例如::before 暗示元素是:

  • 样式或
  • 在某些内容之前插入

因此该元素本质上是动态的,为了识别您必须为所需的 visibilityOfElementLocated() 引入 WebDriverWait 的元素,您可以使用以下任一定位器策略:

  • 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='数据容器']")));

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

解决方案

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']")));
    

这篇关于无法通过 ::before 在 div 内通过 xpath 获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆