【问题标题】:How to make xPath for ul li a href tags如何为 ul li a href 标签制作 xPath
【发布时间】:2021-08-21 11:03:22
【问题描述】:

我正在处理这个项目,我需要验证列表中的每个项目是否已加载到页面上。但是我有点困惑如何创建 xpath,因为文本位于标签内。

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

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

【问题讨论】:

    标签: selenium selenium-webdriver xpath html-lists


    【解决方案1】:

    您应该使用预期条件 - 等待元素可见,而不是您现在正在执行的操作,因为 driver.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 元素?也许缺少一些延迟?
    猜你喜欢
    • 2021-03-03
    • 1970-01-01
    • 2018-03-25
    • 2017-06-04
    • 1970-01-01
    • 2018-11-24
    • 1970-01-01
    • 2018-03-27
    • 2019-01-24
    相关资源
    最近更新 更多