我有一个场景,在这个场景中,我想单击一个超链接,以获得页面上的合同号来打开另一个窗口。我最初遇到的问题是,这个合同号需要几秒钟才能在页面上显示出来。因此,我想我应该利用一个流畅的等待,每隔几秒钟对元素进行轮询,直到它变得可见(我想避免使用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)所以,我要达到的是等待一个合同号码显示在列表上,然后我会点击它。我为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());
}然而,当我运行测试时,我在控制台中得到一个错误,如下所示:
“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'

发布于 2014-12-10 17:27:08
您正在检查a元素的a。您应该使用outerHTML进行测试。innerHTML值将永远不会包含属性,因此也不会包含您正在测试的_blank值。(嗯,除非您可以有一个名为_blank的合同,这看起来不太可能。)让我们举一个例子:
<a class="" target="_blank" href="/DibsAndrew/CreditControl/AgreementDetail.aspx?contractno=254052">254052</a>上面的innerHTML是254052,因为innerHTML只覆盖元素中的内容。outerHTML是上面的整个字符串,因为outerHTML覆盖了元素本身以及它的内容。
好的,所以我上面的建议会解决眼前的问题。但是,我强烈建议将您的测试更改为比匹配中的字符串更好的测试。例如,如果只在加载契约之后才将a元素添加到其父单元格中,则应测试单元格中是否存在a元素。
https://stackoverflow.com/questions/27400290
复制相似问题