【问题标题】:How to use XPATH to search for a span with a class for Selenium Testing in JAVA如何使用 XPATH 在 JAVA 中搜索带有 Selenium 测试类的跨度
【发布时间】:2015-01-31 20:18:22
【问题描述】:

我正在编写一段代码,希望我选择类amountCharged 的跨度并获取该跨度的值以针对模拟值对其进行测试。我面临的问题是我似乎无法使用XPath 选择span。我尝试了多种方法,但仍然失败,我继续问 W3SchoolsStackoverflow 问题,但无法确定语法。

以下是我面临的错误:

org.openqa.selenium.TimeoutException: Timed out after 60 seconds waiting for text ('£163.06') to be present in element found by `By.xpath:`

 //span[contains(@class, 'amountCharged')]

如您所见,我正在尝试将//span[contains(@class,'amountCharged')] 用作XPATH,与使用“”相同的问题

/descendant::span[contains(@class, 'amountCharged')]

从父 DIV 开始的 HTML 是:

    <div class="panelMessage instructional" params="">    Total:         
 <span class="amountCharged">£163.06</span> quoted on          
<span class="reservationTime">Tue, 29 Nov 2011 15:46 GMT</span> . </div>
    </div>        
<div class="panelContent hideFocus noneBlock  " tabindex="-1" data-  context="panelContent">

JAVA代码为:

   private static void elementShouldContain(String locator, String value, String errorMessage, long timeout) {
    String xpath = buildLocator(locator, "");
    WebDriverWait customWait = new WebDriverWait(webDriverInstance, timeout / 1000);
    try {
    customWait.until(ExpectedConditions.textToBePresentInElement(By.xpath(xpath), value));
    } catch (Exception e) {
        // Handles the specific exception, except that the message is null, in which case throws a regular SeleniumException
        if (errorMessage != null)
            throw new CustomSeleniumException(errorMessage, e);
        else
            throw new SeleniumException(e);
    }
}

我错过了什么请帮忙。

谢谢

【问题讨论】:

  • 最好显示要从中选择 XPath 的 HTML。
  • 您的错误消息表明它找不到特定的文本,但它确实找到了元素
  • 我已经粘贴了 HTML,文本 163.06 在那里
  • 是否还有更多带有amountCharged 名称的元素存在?
  • 不,在整个 HTML 中,这个类只有一个跨度。当我通过 chrome reg x checker 进行操作时,它会选择跨度

标签: java selenium xpath


【解决方案1】:

记住这一点的几件事:

  1. 在 Firebug 的 Chrome 调试器上测试 xpath 并不一定意味着 selector 将唯一地返回目标元素。可能有一些其他元素隐藏在相同的选择器中,webdriver 在这种情况下将无法找到目标元素。出于调试目的,请使用 findElements 来查看它返回了多少元素。
  2. 元素加载问题,例如:可见性也可能是另一个问题。这就是为什么您需要确保元素存在且可见的原因。使用explicit等待

尝试关注xpaths

//span[@class='amountCharged']

如果类不是唯一的,并且您想根据 div 查找 span,您可以执行以下操作:

//div[contains(@class,'panelMessage ')]//span[@class='amountCharged']

【讨论】:

  • 我实际上使用 Chrome 调试器让它工作,但我的测试仍然认为它不存在。 /descendant::span[contains(@class, 'amountCharged')]/text()[last()]
  • 我想看看测试代码和stacktrace你得到然后
  • @alyn000r 如果您想得到答案,请显示完整 HTML 和测试代码。我们不喜欢处于黑暗中(我想你也不喜欢)。
  • 这里已经添加了JAVA代码太抱歉没有早点添加。由于公司政策,我无法放置整个 HTML。
【解决方案2】:

根据您提到的错误,我想您正在使用textToBePresentInElementtextToBePresentInElementLocatedExpectedConditions class 下的其他相关方法

您也可以这样做:
1- 使用 Explicit Wait 等待元素的可见性
2- 检索文本
3- 断言/比较文本与值:'£163.06'。

您可以按顺序尝试以下包含以上所有内容的代码:

    String text_in_panel=null;
    try{
        //waiting 20 seconds for the visibility of the element
        WebDriverWait wait = new WebDriverWait(driver, 20);
        WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(@class, 'panelMessage')]/span[1]")));

        //Retrieving the text from the element
        text_in_panel = element.getText();
    }catch(Throwable e){
        System.err.println("Element is not visible: "+e.getMessage());
    }

    //Comparing the text with the hard-coded value
    if(text_in_panel.equals("£163.06"))
        System.out.println("Text matches");
    else
        System.err.println("Text doesn't match");

[或者您可以通过导入 import junit.framework.Assert 使用 Assert 类来断言值而不是比较它们;]

    //Assert the text with the hard-coded value
    try{
        Assert.assertEquals(text_in_panel, "£163.06");
        System.out.println("Text matches");
    }catch(Throwable e){
        System.err.println("Text doesn't match");
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多