Finding the searched text in google search result with selenium webdriver in Chrome

I am new to selenium webdriver and having a problem

i googled something and the result i want to see if the first 5 results contain the searched text or not.

as example:

i want to search for the text "selenium webdriver". now i want to check if the first 5 results contains the text "selenium webdriver" or not.

Here is my part:


IWebElement searchText = chromeDriver.FindElement(By.XPath(".//html/body/div[3]/div/div/div[2]/div[2]/div/form/fieldset[2]/div/div/div/table/tbody/tr/td[2]/div/input"));
searchText.SendKeys("selenium webdriver");

IWebElement searchButton = chromeDriver.FindElement(By.Name("btnG"));
searchButton.Click() ;

IWebElement resultingText = chromeDriver.FindElement(By.LinkText("selenium webdriver"));

this line is throwing an exception :

// IWebElement resultingText = chromeDriver.FindElement(By.LinkText("selenium webdriver"));

can any one help me in this issue?


You're getting the exception because a LinkText locator will find an exact match of the link text, so it's looking for a link that contains only the text "selenium webdriver". If you look at the result of the Google search, an exact match is not present, so you get the exception.

This code will print out every link contained within the first page of the search results. From here, you can modify this to check the first five to see if it contains text that matches your criteria:

IWebDriver driver = new ChromeDriver();

IWebElement element = driver.FindElement(By.Id("gbqfq"));
element.SendKeys("selenium webdriver");

// Get the search results panel that contains the link for each result.
IWebElement resultsPanel = driver.FindElement(By.Id("search"));

// Get all the links only contained within the search result panel.
ReadOnlyCollection<IWebElement> searchResults = resultsPanel.FindElements(By.XPath(".//a"));

// Print the text for every link in the search results.
foreach (IWebElement result in searchResults)

  1. You're using terrible xpath. Try something like //input[@id='gbqfq']
  2. When you're searching something by text, it is registry sensitive
  3. For your case with 5 results, you can use the following (I'm not familiar with C#, so here's the python code, you should understand it):

    first_five = driver.find_elements_by_xpath(".//*[@id='rso']//div//h3/a")[:5]     
    #returns the list of first five result links
    for result in first_five:
        assert "selenium webriver" in result.text.lower(), "Result does not contain 'selenium webdriver'" 
        # lower - to get rid off registry troubles

It is advisable and better to use CSS selector or if you are willing to use only XPath. please choose 'fire path'. which generates short and precise Xpath and place it in your code. avoid using such lengthy Xpath.

I have solved this problem using, selenium. It works for me. I hope this might be helpful for reference.

Imports OpenQA.Selenium'
Module SearchResults
Sub Main()
        Dim Browser As Chrome.ChromeDriver = New Chrome.ChromeDriver("C:\SeleniumLive\DriverFiles\")
        For i = 0 To 4
            If Browser.FindElement(By.Id("lst-ib")) Is Nothing Then
            End If
        If Browser.FindElement(By.Id("lst-ib")) IsNot Nothing Then
            Browser.FindElement(By.Id("lst-ib")).SendKeys("Sumit Shitole" & Keys.Enter)
        End If
        For pageNumber As Integer = 1 To 8 Step 1
            Console.WriteLine("......Result from page " & pageNumber & "..........")
            Dim results As New List(Of IWebElement)(Browser.FindElements(By.CssSelector(".r>a")))
            For Each result As IWebElement In results
    Catch ex As Exception
    End Try
End Sub
End Module

Need Your Help

Is there way to change executable path when debugging crashdumps?

visual-studio-2010 visual-c++ visual-studio-debugging crash-dumps minidump

While trying to debug minidumps of our application taken during crashes on customer PCs, I find that the path where they install the application seem to have an effect on where my executable should...

Architecture in Eclipse

eclipse assembly x86 pdp-11

How can I change architecture in Eclipse from x86 to pdp11, what flags do I need, and do I need adjust cross-compiler, thanks in advance