我有一个场景,我想单击页面上契约(Contract)编号的超链接来打开另一个窗口。我最初遇到的问题是,该契约(Contract)编号需要几秒钟的时间才能在页面上可见。因此,我想我应该利用流畅的等待每隔几秒轮询一次对元素的搜索,直到它变得可见(我想避免使用 Thread.Sleep)。但是,我在让它发挥作用时遇到了问题。我在下面详细介绍了我所做的事情
1) 请参阅下面的我尝试单击的合约的屏幕截图 (254052)。这是合约,需要几秒钟的时间才会出现在页面上
相关的 HTML 如下:
<div id="tabular-breakdown" class="tablesorter" style="display: block;">
<table class="tablesorterReport" data-currentpage="0" data-totalcount="696" data-totalpages="35">
<thead>
<tbody>
<tr>
<td>
<a class="" target="_blank" href="/DibsAndrew/CreditControl/AgreementDetail.aspx?contractno=254052">254052</a>
2)所以,我想要实现的是等待契约(Contract)号显示在列表上,然后我将单击它。我已经编写了 FluentWait 的代码,如下所示:
public static void fluentWaitOnContractSelect(InternetExplorerDriver driver)
{
FluentWait<InternetExplorerDriver> wait = new FluentWait<InternetExplorerDriver>(driver);
wait.withTimeout(8000, TimeUnit.MILLISECONDS);
wait.pollingEvery(10, TimeUnit.MILLISECONDS);
wait.ignoring(NoSuchElementException.class);
wait.ignoring(StaleElementReferenceException.class);
WebElement contractSelect = wait.until(new Function<InternetExplorerDriver, WebElement>(){
public WebElement apply(InternetExplorerDriver driver) {
WebElement contract = driver.findElement(By.xpath(".//*[@id='tabular-breakdown']/table/tbody/tr[1]/td[1]/a"));
String value = contract.getAttribute("innerHTML");
if(value.contains("_blank"))
{
contract.click();
return contract;
}
else
{
System.out.println("Value is " + value);
return null;
}
}
});
System.out.println("Webelement value is " + contractSelect.getTagName());
}
3)但是,当我运行测试时,我在控制台中收到错误,如下所示:
“exception in thread “main” org.openqa.selenium.TimeoutException: Timed out after 8 seconds waiting for genericControls.waitCommands$1@1b68b9a4
Build info: version: ‘2.43.1’, revision: ‘5163bce’, time: ‘2014-09-10 16:27:58′
System info: host: ‘MBD0150′, ip: ‘192.168.55.49’, os.name: ‘Windows 8.1′, os.arch: ‘amd64′, os.version: ‘6.3’, java.version: ‘1.8.0_25′
Driver info: driver.version: unknown
at org.openqa.selenium.support.ui.FluentWait.timeoutException(FluentWait.java:259)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:228)
at genericControls.waitCommands.fluentWaitOnContractSelect(waitCommands.java:24)
at genericControls.contractFunctions.openCloseContractForReportWithBarChart(contractFunctions.java:41)
at Reports.collections.breachReportCompletedSelectContract(collections.java:55)
at Reports.programMain.main(programMain.java:117)
Caused by: org.openqa.selenium.StaleElementReferenceException: Element is no longer valid (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 57 milliseconds
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Session ID: e9003b03-0004-4846-a0b9-6991cadac9b0
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
at org.openqa.selenium.remote.RemoteWebElement.getAttribute(RemoteWebElement.java:123)
at genericControls.waitCommands$1.apply(waitCommands.java:28)
at genericControls.waitCommands$1.apply(waitCommands.java:1)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:208)”
有人对我可能出错的地方有任何想法吗?
非常感谢
'eclipse-10/12/14'
最佳答案
您正在检查 a
元素的 innerHTML
。您应该改为针对 outerHTML
进行测试。 innerHTML
值绝不会包含属性,因此绝不会包含您正在测试的_blank
值。 (好吧,除非你可以有一个名为 _blank
的合约,但这似乎不太可能。)让我们举个例子:
<a class="" target="_blank" href="/DibsAndrew/CreditControl/AgreementDetail.aspx?contractno=254052">254052</a>
上面的 innerHTML
是 254052
,因为 innerHTML
仅覆盖元素内部的内容。 outerHTML
是上面的整个字符串,因为 outerHTML
覆盖了元素本身及其内容。
好的,我上面的建议可以解决眼前的问题。但是,我强烈建议将您的测试更改为比匹配 HTML 中的字符串更好的测试。例如,如果仅在加载合约后才将 a
元素添加到其父单元格中,则您应该测试单元格中是否存在 a
元素。
关于java - 如何在 webdriver 中有效地使用流畅的等待命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27400290/