WebDriver getText 抛出异常
好吧,我完全不知道: 我在页面上有一个表格,其中每行的 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>
整行都是可点击的,因此在我发送 click() 命令后,它将打开我找到的拍卖
有什么奇怪的: auctionRow .getText(); 抛出错误。
如果我将其更改为 getTagName() 函数,它将正确返回我“span”
我如何强制它为我提供两个跨度之间的文本?
Stak 追踪:
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);
}
Okay, I am completly clueless:
I have table on the page where every rows has CSS ID incremented by one. And I am searching the Auction ID in such table and matching it with the Auction I entered through previous Selenium test. So my code goes like this:
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();
}
Where "auctionID" is send to the method by previous test.
auctionRow is Webelement represented with two spans where the actual auctionID is stored
<span id="mainForm:aucPanelId:0:listOfAuctions:0">116</span>
Whole row is clickable, so after i send the click() command, it will open me found auction
What is strange: The auctionRow.getText(); throws an error.
If i change it to getTagName() function it will correctly return me "span"
How do I force it to provide me the text between two spans?
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)
SOLVED
Ok, I found nice and easy (and shorter to code) workaround. Since I know that the Auction ID is in span element an I know what the ID should be, I am now searching by 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不会这样做:
我会尝试这样的事情:
或者一些类似的想法,具体取决于你在做什么。
Instead of doing this:
I would try something like this:
Or some similar idea, depending on what you are doing.
在许多情况下,最好调用 getValue(),而不是使用 getText()。使用 getValue 您可能会得到更长的字符串,但您期望的文本应该在那里。
Instead of using getText(), in many occasions it's better to call getValue(). Using the getValue you might get a longer string, but the text you are expecting inside should be there.