1
votes

I am trying to retrieve the search results from YouTube suggestions using Selenium webdriver (Java) but i am getting the following error. Please help.

Error:

Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"class name","selector":"gssb_e"}

Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:18:15' System info: host: 'localhost', ip: '127.0.0.1', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.9.3', java.version: '1.7.0_55' Driver info: driver.version: unknown

Code

package test3;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class YoutubeSuggest {

    public static void main(String[] args) {
                // The Firefox driver supports javascript 
                WebDriver driver = new FirefoxDriver();
                
                // Go to the youtube home page
                driver.get("http://www.youtube.com/");
                
                // Enter the query string "Ozzy "
                WebElement query = driver.findElement(By.name("search_query"));
                query.sendKeys("Ozzy ");
                
                // Sleep until the div we want is visible or 10 seconds is over
                long end = System.currentTimeMillis() + 10000;
                while (System.currentTimeMillis() < end) {
                    WebElement resultsDiv = driver.findElement(By.className("gssb_e"));

                    // If results have been returned, the results are displayed in a drop down.
                    if (resultsDiv.isDisplayed()) {
                      break;
                    }
                }

                // And now list the suggestions
                List<WebElement> allSuggestions = driver.findElements(By.xpath("//td[@class='gsq_a']"));
                
                for (WebElement suggestion : allSuggestions) {
                    System.out.println(suggestion.getText());
                }

                //driver.quit();
    }

}

HTML generated by youtube suggestions:

<table dir="ltr" class="gstl_50 gssb_c"
    style="width: 585px; top: 39px; position: fixed; text-align: left; left: 230px; display: none;"
    cellpadding="0" cellspacing="0">
    <tbody>
        <tr>
            <td class="gssb_f"></td>
            <td style="width: 100%;" class="gssb_e"><table class="gssb_m"
                    style="width: 100%;" cellpadding="0" cellspacing="0">
                    <tbody>
                        <tr class="">
                            <td style="text-align: left;" dir="ltr" class="gssb_a gsfs"><div
                                    class="gsq_a">
                                    <table style="width: 100%;" cellpadding="0" cellspacing="0">
                                        <tbody>
                                            <tr>
                                                <td style="width: 100%;"><span>ozzy<b>
                                                            osbourne</b></span></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div></td>
                        </tr>
                        <tr class="">
                            <td style="text-align: left;" dir="ltr" class="gssb_a gsfs"><div
                                    class="gsq_a">
                                    <table style="width: 100%;" cellpadding="0" cellspacing="0">
                                        <tbody>
                                            <tr>
                                                <td style="width: 100%;"><span>ozzy<b>
                                                            osbourne crazy train</b></span></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div></td>
                        </tr>
                        <tr class="">
                            <td style="text-align: left;" dir="ltr" class="gssb_a gsfs"><div
                                    class="gsq_a">
                                    <table style="width: 100%;" cellpadding="0" cellspacing="0">
                                        <tbody>
                                            <tr>
                                                <td style="width: 100%;"><span>ozzy<b>
                                                            osbourne no more tears</b></span></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div></td>
                        </tr>
                        <tr class="">
                            <td style="text-align: left;" dir="ltr" class="gssb_a gsfs"><div
                                    class="gsq_a">
                                    <table style="width: 100%;" cellpadding="0" cellspacing="0">
                                        <tbody>
                                            <tr>
                                                <td style="width: 100%;"><span>ozzy<b>
                                                            osbourne mama i'm coming home</b></span></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div></td>
                        </tr>
                        <tr class="">
                            <td style="text-align: left;" dir="ltr" class="gssb_a gsfs"><div
                                    class="gsq_a">
                                    <table style="width: 100%;" cellpadding="0" cellspacing="0">
                                        <tbody>
                                            <tr>
                                                <td style="width: 100%;"><span>ozzy<b>
                                                            osbourne paranoid</b></span></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div></td>
                        </tr>
                        <tr class="">
                            <td style="text-align: left;" dir="ltr" class="gssb_a gsfs"><div
                                    class="gsq_a">
                                    <table style="width: 100%;" cellpadding="0" cellspacing="0">
                                        <tbody>
                                            <tr>
                                                <td style="width: 100%;"><span>ozzy<b>
                                                            osbourne justin bieber</b></span></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div></td>
                        </tr>
                        <tr class="">
                            <td style="text-align: left;" dir="ltr" class="gssb_a gsfs"><div
                                    class="gsq_a">
                                    <table style="width: 100%;" cellpadding="0" cellspacing="0">
                                        <tbody>
                                            <tr>
                                                <td style="width: 100%;"><span>ozzy<b>
                                                            osbourne mr crowley</b></span></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div></td>
                        </tr>
                        <tr class="">
                            <td style="text-align: left;" dir="ltr" class="gssb_a gsfs"><div
                                    class="gsq_a">
                                    <table style="width: 100%;" cellpadding="0" cellspacing="0">
                                        <tbody>
                                            <tr>
                                                <td style="width: 100%;"><span>ozzy<b>
                                                            osbourne bark at the moon</b></span></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div></td>
                        </tr>
                        <tr class="">
                            <td style="text-align: left;" dir="ltr" class="gssb_a gsfs"><div
                                    class="gsq_a">
                                    <table style="width: 100%;" cellpadding="0" cellspacing="0">
                                        <tbody>
                                            <tr>
                                                <td style="width: 100%;"><span>ozzy<b>
                                                            osbourne interview</b></span></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div></td>
                        </tr>
                        <tr class="">
                            <td style="text-align: left;" dir="ltr" class="gssb_a gsfs"><div
                                    class="gsq_a">
                                    <table style="width: 100%;" cellpadding="0" cellspacing="0">
                                        <tbody>
                                            <tr>
                                                <td style="width: 100%;"><span>ozzy<b>
                                                            osbourne dreamer</b></span></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div></td>
                        </tr>
                    </tbody>
                </table></td>
        </tr>
    </tbody>
</table>
2
How on earth do you want to do that from UI level? Is there a reason you can't use Youtube API? - Yi Zeng
I am trying to do something similar to the code @ code.google.com/p/selenium/wiki/GettingStarted, which works but the youtube suggestion isn't working. This is more for automation using web driver. - kasukur
YouTube or even any Google based site, is just awful to use for site automation. I'd try something different, if all you are attempting to do is use WebDriver on something. - Arran

2 Answers

0
votes

Try with below logic to get the youtube search results after loading URL .

driver.findElement(By.name("search_query")).sendKeys("Selenium WebDriver"+Keys.ENTER);
List<WebElement> search_result=driver.findElements(By.cssSelector("#results ol#search-results>li h3>a"));
for(WebElement each_result:search_result) {
   System.out.println(each_result.getAttribute("title"));
}
0
votes

it seems that since the time of writing the tutorial google has changed the element class names. The changes below worked for me.

WebElement resultsDiv = driver.findElement(By.className("sbsb_a"));
 List allSuggestions = driver.findElements(By.xpath("//li[@class='sbsb_c gsfs']"));