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

10,607

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();
driver.Navigate().GoToUrl("http://google.com");

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)
{
    Console.WriteLine(result.Text);
}
Share:
10,607
user2501321
Author by

user2501321

Updated on June 04, 2022

Comments

  • user2501321
    user2501321 over 1 year

    I am new to selenium webdriver and having a problem

    i googled something and the result appeared.now 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:

    chromeDriver.Navigate().GoToUrl("http://www.google.co.uk");
    
    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?

    • user2501321
      user2501321 over 10 years
      OpenQA.Selenium.NoSuchElementException: The element could not be found
    • Arran
      Arran over 10 years
      You want to check if the first 5 results contain "selenium webdriver", and is that it? What do you want to do? Click the first one?
    • user2501321
      user2501321 over 10 years
      to click any of the five. but first of all it should be checked that if they contain the text or not.
    • user2501321
      user2501321 over 10 years
      please help . i don't get what is the prob with this code
    • Arran
      Arran over 10 years
      Post a screenshot & the HTML of what link you intend to click on.
  • user2501321
    user2501321 over 10 years
    thank you. thanks for the effort . it's working.:) @PocketDews