簡體   English   中英

無法通過 ::before 在 div 內通過 xpath 獲取元素

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

我需要使用 Web 驅動程序對象獲取 Web 元素列表

findElements(By.xpath(""));

我通過使用 xpath 作為//*[@class=\\"providers-list clearfix\\"]來獲取//*[@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" ]"}

存在,例如::before意味着該元素是:

  • 風格或
  • 在某些內容之前插入

因此,元素本質上是動態的,為了識別元素,您必須誘導WebDriverWait以獲得所需的visibilityOfElementLocated() ,您可以使用以下任一定位器策略

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

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

您是否試圖從 findElements 的對象訪問? 這是它理想的工作方式:

    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