好吧,我完全无能为力: 我在页面上有表,其中每行的CSS ID都加1。我正在这样的表中搜索拍卖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是以两个跨度表示的Webelement,其中存储了实际的auctionID
<span id="mainForm:aucPanelId:0:listOfAuctions:0">116</span>
整行是可点击的,所以在我发送点击()命令后,它会打开我发现的拍卖
奇怪的是: auctionRow.getText(); 会引发错误。
如果我将其更改为 getTagName()功能,它将正确地返回“span”
如何强制它在两个跨度之间提供文本?
Stak Trace:
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);
}
答案 0 :(得分:0)
而不是这样做:
getDriver().findElement(By.xpath("//span[text()='"+auctionID+"']"));
我会尝试这样的事情:
By auctionList = By.xpath(".//span[contains(@id,'listOfAuctions')]"));
By containsTextWithId = By.xpath(".//span[contains(.,'" + id + "')]"));
By combinedLocator = new ByChained(auctionList, containsTextWithId);
或者类似的想法,取决于你在做什么。
答案 1 :(得分:0)
在很多情况下,调用 getValue() 比使用 getText() 更好。使用 getValue 您可能会得到一个更长的字符串,但您所期望的文本应该在那里。