1

I am setting up a test to perform a search and after the search is complete, i want to capture the results line that says "About xxx results (x.xx seconds)". I can get this to work using the firefox webdriver, but it does not work when I use IE or Chrome driver. I need help determining what is wrong with my code. Here is the code snippet:

public void runSearch(WebDriver driver) {

    WebElement element = driver.findElement(By.name("search"));
    element.sendKeys("Kearney");
    element.sendKeys(Keys.RETURN);
    String itext = driver.findElement( By.cssSelector("div#resInfo-0")).getText();
    System.out.println("Search returned '" + itext + "'.");
}

Here is the html of my page after the search has been made:

<head></head>
<body>
    <header id="main_header_iphone"></header>
    <div class="clear"></div>
    <header id="main-header"></header>
    <section>
        <div class="content_nn">
            <div class="subnav">
                <!--

                <div class="bannerBox">
                 <div class="btn_events"><…

                -->
                <div class="logo"></div>
                <!--

                START Search

                -->
                <div class="search_container">
                    <div id="cse" style="width:100%;">
                        <div class="gsc-control-cse gsc-control-cse-en">
                            <div class="gsc-control-wrapper-cse" dir="ltr">
                                <form class="gsc-search-box" accept-charset="utf-8"></form>
                                <div class="gsc-results-wrapper-nooverlay gsc-results-wrapper-visible">
                                    <div class="gsc-tabsAreaInvisible"></div>
                                    <div class="gsc-tabsAreaInvisible"></div>
                                    <div class="gsc-above-wrapper-area">
                                        <table class="gsc-above-wrapper-area-container" cellspacing="0" cellpadding="0">
                                            <tbody>
                                                <tr>
                                                    <td class="gsc-result-info-container">
                                                        <div id="resInfo-0" class="gsc-result-info">

                                                            About 31 results (0.11 seconds)

                                                        </div>
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                    <div class="gsc-adBlockNoHeight" style="height: 0px; font-weight: normal; text-align: center;"></div>
                                    <div class="gsc-wrapper"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
                    <script type="text/javascript"></script>
                    <script type="text/javascript" src="http://www.google.com/uds/?file=search&v=1&hl=en"></script>
                    <link rel="stylesheet" type="text/css" href="http://www.google.com/uds/api/search/1.0/65b21018ad4df09e3eb5a21326b72d0b/default+en.css"></link>
                    <script type="text/javascript" src="http://www.google.com/uds/api/search/1.0/65b21018ad4df09e3eb5a21326b72d0b/default+en.I.js"></script>
                    <!--

                    END Search

                    -->
                </div>
                <div class="text_rural_h2" style="float:left; width:100%"></div>
                <div class="index_page_heading" style="float:left;"></div>
                <div class="letest_news2" style="float:left;"></div>
            </div>
            <footer id="footer"></footer>
        </div>
    </section>
    <div id="topcontrol" style="position: fixed; bottom: 25px; right: 10px; opacity: 0; cursor: pointer;" title="Scroll Back to Top"></div>
    <table class="gstl_50 gssb_c" cellspacing="0" cellpadding="0" style="width: 137px; display: none; top: 153px; position: absolute; left: 97px;"></table>
    <div style="display:none"></div>
</body>

1
  • In the code the line: "String itext = driver.findElement( By.cssSelector("div#resInfo-0")).getText();" works when I use the firefox driver and does not work when I use chrome or IE. I don't understand how the different browsers work and I am not sure why I am getting different results. Any suggestions? Commented Dec 30, 2013 at 16:40

2 Answers 2

1

It is possible, the search result page takes more time for loading than WebDriver expects.
I propose to wait until the next page is loaded with WebDriverWait methods.

element.sendKeys("Kearney");
element.sendKeys(Keys.RETURN);

// Wait! 
WebDriverWait wait = new WebDriverWait(driver, 20);
By waitFor = By.cssSelector("div#resInfo-0");
WebElement lblSearchResults = wait.until(ExpectedConditions.ElementIsVisible(waitFor));
// 

String itext = lblSearchResults.getText();

Please see also this topic - WebDriver: Advanced Usage

Sign up to request clarification or add additional context in comments.

3 Comments

I put your code in as you listed it and I get an error on ElementIsvisible and I am not sure what to do with it. Here is the error I receive: "The method ElementIsVisible(By) is undefined for the type ExpectedConditions". How to I resolve this error? I do get two quick fixes for the error, but neither appears to be correct. What should I do?
After looking at this some more, I was able to take your suggestion and modify it to make it work. You are correct that the search was taking more time than the webdriver expected and I was able to resolve it with this code:
public void runSearch(WebDriver driver) { // Execute google search WebElement element = driver.findElement(By.name("search")); element.sendKeys("Kearney"); element.sendKeys(Keys.RETURN); // Wait! By waitFor = By.id("resInfo-0"); WebElement lblSearchResults = driver.findElement(waitFor); // // get and print out search results String itext = lblSearchResults.getText(); System.out.println("Search returned '" + itext + "'.");'code'
0

For IE and Chrome you need an extra driver. Start with the chrome driver and check if this solves you problem.

2 Comments

the extra driver is already setup when I use chrome or IE. That is not the issue. The issue has something to do with how it interprets the HTML. as everything works in my test except the line that says "By waitFor = By.cssSelector("div#resInfo-0");". This line fails when I use IT or chrome, but when I use firefox it returns the answer I want. What else could be the problem?
So you already gave the WebDriverWait a try? What is the exception?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.