简体   繁体   English

WebDriver getText 抛出异常

[英]WebDriver getText throws exceptions

Okay, I am completly clueless: I have table on the page where every rows has CSS ID incremented by one.好吧,我完全一无所知:我在页面上有一张表格,其中每一行的 CSS ID 都增加了 1。 And I am searching the Auction ID in such table and matching it with the Auction I entered through previous Selenium test.我正在该表中搜索拍卖 ID,并将其与我通过之前的 Selenium 测试输入的拍卖进行匹配。 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.其中“auctionID”通过先前的测试发送到方法。

auctionRow is Webelement represented with two spans where the actual auctionID is stored拍卖行是用两个跨度表示的 Web 元素,其中存储了实际的拍卖 ID

  <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整行都是可点击的,所以在我发送click()命令后,它会打开我发现的拍卖

What is strange: The auctionRow.getText();奇怪的是: auctionRow.getText(); throws an error.引发错误。

If i change it to getTagName() function it will correctly return me "span"如果我将其更改为getTagName()函数,它将正确返回我“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:由于我知道拍卖 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);
    }

Instead of doing this:而不是这样做:

getDriver().findElement(By.xpath("//span[text()='"+auctionID+"']"));

I would try something like this:我会尝试这样的事情:

By auctionList = By.xpath(".//span[contains(@id,'listOfAuctions')]"));
By containsTextWithId = By.xpath(".//span[contains(.,'" + id + "')]"));
By combinedLocator = new ByChained(auctionList, containsTextWithId);

Or some similar idea, depending on what you are doing.或者一些类似的想法,这取决于你在做什么。

Instead of using getText(), in many occasions it's better to call getValue().在许多情况下,最好调用 getValue(),而不是使用 getText()。 Using the getValue you might get a longer string, but the text you are expecting inside should be there.使用 getValue 你可能会得到一个更长的字符串,但你期望的文本应该在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM