Selenium 如何为ul li a href标记生成xPath

Selenium 如何为ul li a href标记生成xPath,selenium,selenium-webdriver,xpath,html-lists,Selenium,Selenium Webdriver,Xpath,Html Lists,我在这个项目上工作,我需要验证列表中的每个项目是否加载到页面上。然而,我有点困惑如何创建xpath,因为文本在标记中 我首先需要获取元素,然后断言是否显示该项。下面的第一行可以工作,但是断言给出了一个错误 WebElement costRequest = driver.findElement(By.xpath("//a[contains(text(),'Cost')]")); Assert.assertEquals(true, costRequest.isDi

我在这个项目上工作,我需要验证列表中的每个项目是否加载到页面上。然而,我有点困惑如何创建xpath,因为文本在标记中

我首先需要获取元素,然后断言是否显示该项。下面的第一行可以工作,但是断言给出了一个错误

    WebElement costRequest = driver.findElement(By.xpath("//a[contains(text(),'Cost')]"));
    Assert.assertEquals(true, costRequest.isDisplayed());
    log.info("Verify cost request");

您应该使用预期条件-等待元素可见,而不是您现在正在执行的操作,因为
驱动程序。findElement
返回元素存在时的web元素,但尚未完成,因此尚未显示该元素。
因此,请这样做:

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(text(),'Cost')]")));
WebElement costRequest = driver.findElement(By.xpath("//a[contains(text(),'Cost')]"));
Assert.assertEquals(true, costRequest.isDisplayed());
    log.info("Verify cost request");

这对我来说仍然不起作用,我怀疑我使用的xpath是不正确的。到底是什么不起作用?是否有多个
a
元素带有“成本”文本?也许有点耽搁了?