简体   繁体   中英

Unable to locate the Search Box element on Google Home Page using Selenium and C#

Why i am getting this error?:

OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"css selector","selector":"#gbqfq"}

This is the code i am using to open the browser and search for a given word in google search:

 IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://google.com");

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

// 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.
int resultCNT = 1;
foreach (IWebElement result in searchResults)
{
    if (resultCNT <= 5)
    {
        Console.WriteLine(result.Text);
    }
    else
    {
        break;
    }
    resultCNT ++;
}

Does somebody know what could be the issue?Thanks

Full Log:

OpenQA.Selenium.NoSuchElementException
  HResult=0x80131500
  Message=no such element: Unable to locate element: {"method":"css selector","selector":"#gbqfq"}
  (Session info: chrome=84.0.4147.125)
  Source=WebDriver
  StackTrace:
   at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementById(String id)
   at OpenQA.Selenium.By.<>c__DisplayClass16_0.<Id>b__0(ISearchContext context)
   at OpenQA.Selenium.By.FindElement(ISearchContext context)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
   at $Program.$Main(String[] args) in C:\Users\gabri\source\repos\Example\Example\Program.cs:line 10

As your usecase is to open the browser and search for a given word within Google Home Page , the Search Box HTML is as follows:

<input class="gLFyf gsfi" maxlength="2048" name="q" type="text" jsaction="paste:puy29d" aria-autocomplete="both" aria-haspopup="false" autocapitalize="off" autocomplete="off" autocorrect="off" autofocus="" role="combobox" spellcheck="false" title="Search" value="" aria-label="Search" data-ved="0ahUKEwiq_ZvUyJvrAhXhzjgGHXBjBx4Q39UDCAQ">

So to identify the <input> box you can use either of the following Locator Strategies :

  • Name :

     driver.FindElement(By.Name("q")).SendKeys("gbqfq");
  • CssSelector :

     driver.FindElement(By.CssSelector("[name='q']")).SendKeys("APPLES");
  • XPath :

     driver.FindElement(By.XPath("//*[@name='q']")).SendKeys("APPLES");

Moreover, the desired element is a JavaScript enabled element, so to send a character sequence within the element you have to induce WebDriverWait for the ElementToBeClickable() and you can use either of the following ocator Strategies :

  • Name :

     new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.Name("q"))).SendKeys("APPLES");
  • CssSelector :

     new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("[name='q']"))).SendKeys("APPLES");
  • XPath :

     new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@name='q']"))).SendKeys("APPLES");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM