【发布时间】:2012-02-07 17:20:54
【问题描述】:
好吧,我完全一无所知: 我在页面上有表格,其中每一行的 CSS ID 都加一。我正在此类表中搜索拍卖 ID,并将其与我通过之前的 Selenium 测试输入的拍卖相匹配。所以我的代码是这样的:
int i = 0;
Boolean stillHaveSomethingToSearch = true;
Boolean foundit = false;
while (stillHaveSomethingToSearch){
idConstructor = "mainForm:aucPanelId:0:listOfAuctions:"+i;
try{
auctionRow = driver.findElement(By.id(idConstructor));
} catch (NoSuchElementException e){
stillHaveSomethingToSearch = false;
}
if (stillHaveSomethingToSearch) {
foundAuctionID = auctionRow.getText();
System.out.println("foundAuctionID = " + foundAuctionID);
if (auctionID.equals(foundAuctionID)){
stillHaveSomethingToSearch = false;
foundit = true;
}
}
i++;
}
if (foundit){
auctionRow.click();
}
“auctionID”是通过先前的测试发送到方法的。
auctionRow 是 Web 元素,用两个 span 表示,其中存储了实际的 auctionID
<span id="mainForm:aucPanelId:0:listOfAuctions:0">116</span>
整行都是可点击的,所以在我发送click()命令后,它会打开我找到的拍卖
奇怪的是:auctionRow.getText(); 抛出错误。
如果我将其更改为 getTagName() 函数,它将正确返回“span”
如何强制它为我提供两个跨度之间的文本?
桩迹:
org.openqa.selenium.WebDriverException: (WARNING: The server did not provide any stacktrace information)
Build info: version: '2.0rc3', revision: 'unknown', time: '2011-06-21 23:22:02'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_20'
Driver info: driver.version: RemoteWebDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:131)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:105)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:402)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:213)
at org.openqa.selenium.remote.RemoteWebElement.getText(RemoteWebElement.java:117)
at com.deutscheboerse.testing.AuctionsPage.showAuctionID(AuctionsPage.java:63)
已解决 好的,我发现了很好且简单(并且代码更短)的解决方法。因为我知道拍卖 ID 在 span 元素中,我知道 ID 应该是什么,所以我现在通过 Xpath 搜索:
public AuctionTab showAuctionID(String auctionID){
try{
auctionRow = getRegulationUI().getDriver().findElement(By.xpath("//span[text()='"+auctionID+"']"));
}catch (NoSuchElementException e){
throw new NotFoundException("Auction ID "+ auctionID+ "was not found on first page");
}
auctionRow.click();
return new AuctionTab(this);
}
【问题讨论】:
-
很高兴您解决了它,作为旁注,我建议您从堆栈跟踪中使用最新版本的 Selenium(我上次检查时为 2.16.1),看起来您使用的是 2.0rc3。有时奇怪的错误可能是已经修复的错误。
标签: java exception selenium webdriver gettext