wade
wade

Reputation: 11

how to find search results using selenium webdriver

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>

Upvotes: 1

Views: 11993

Answers (2)

Dmytro Zharii
Dmytro Zharii

Reputation: 291

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

Upvotes: 1

Spindizzy
Spindizzy

Reputation: 8924

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

Upvotes: 0

Related Questions