2014-10-27 104 views
-1

试图从span元素中获取数字动态文本值。我的测试是数据驱动的,因此每次运行时都需要从网页获取文本(金额),然后将其与Excel中的期望值进行比较。出于某种原因,我的代码不起作用。请帮忙解决。Selenium WebDriver Java - 无法从span标记中获取动态文本

我的HTML:

$ 445.87

我的定位:按价格= By.xpath(“// DIV [@类= '价格NG结合'] [@ NG隐藏= 'calc.isCalculating'] /文本()“);

我的代码:

WebDriverWait wait = new WebDriverWait(driver, 15); 
    wait.until(ExpectedConditions.presenceOfElementLocated(PRICE)); 

    WebElement actualPriceElm = driver.findElement(PRICE); 
    actualPriceElm.getText(); 

    Assert.assertEquals(actualPriceElm, strExpectedPrice); 

我的错误:超时15秒等待元件的存在之后通过位于:By.xpath:// DIV [@类= '价格纳克结合'] [@当我使用我的xpath而没有像/ div [@ class ='price ng-binding'] [@ ng-binding('隐藏='calc.isCalculating']然后我得到以下错误:

线程“主”java.lang.AssertionError异常:预期[445.87]但找到[[[FirefoxDriver:firefox在WINDOWS上(f26e0c00-823f-4d45-8285-014303527524)] - > xpath:// div [@ class ='price ng-binding'] [@ ng-hide ='calc.isCalculating']]]

+0

这是HTML:

$ 445.87
Russ 2014-10-27 23:28:53

+0

等待之前,是什么导致元素出现?在手动测试中,您会如何制作动态元素? – CharlieS 2014-10-27 23:33:39

+0

尝试使用'By.CssSelector()' – vkrams 2014-10-27 23:33:44

回答

0

找到解决方案。正如Vikram在他的评论中提到的,我不得不使用cssSelector而不是xPath。 (“div.priceLine:nth-​​child(1)> div:nth-​​child(4)> div:nth-​​child(2)”); VS. By.xpath(“// div [@ class ='price ng-binding'] [@ ng-hide ='calc.isCalculating']”);

感谢大家的建议和意见。

0
Wait<WebDriver> wait = new WebDriverWait(driver, 50); 

// DO SOMETHING HERE TO CAUSE ELEMENT TO BE DISPLAYED (ie click, hover, mousemove, etc) 

wait.until(new Function<WebDriver, Boolean>() { 
    public Boolean apply(WebDriver driver) { 
     return (driver.findElement(By.xpath("//div[@class='price ng-binding'][@ng-hide='calc.isCalculating']/text()"))).isDisplayed(); 
    } 
}); 
+0

由于某种原因,我不能插入这段代码。我得到如下:方法直到(Function <?超级webdriver的,T>)在类型等待不适用的参数(新功能(){})。哪个功能包是那个? – Russ 2014-10-28 18:13:01

+0

com.google.common.base.Function – CharlieS 2014-10-28 23:27:57

+0

您可以使用自己的wait.until(),关键是要做些什么来使页面动态更新.. – CharlieS 2014-10-28 23:30:28

0

您的xpath //div[@class='price ng-binding'][@ng-hide='calc.isCalculating']/text()返回文本445.87,而您需要查找WebElement并获取其文本。您是否尝试使用Assert.assertEquals(actualPriceElm.getText(), "445.87");

+0

好吧,我试过了,但是又没有运气。 \t \t WebDriverWait等待=新WebDriverWait(驱动程序,60); \t \t wait.until(ExpectedConditions.textToBePresentInElementLocated(PRICE,expectedPrice)); WebElement actualPriceElm = driver.findElement(PRICE); Assert.assertEquals(actualPriceElm.getText(),expectedPrice);错误我得到:60秒等待文本(“445.87”)存在于由By.xpath发现元件后超时:// DIV [@类=“价格纳克结合”] [@纳克隐藏= 'calc.isCalculating'] – Russ 2014-10-31 22:10:27

相关问题