selenium-webdriverselenium-ideselenium-gridxpath-2.0

Unable to Find the Xpath for Google Home Page textarea or Input


Unable to find the XPath for google home page - Search box.

  1. Open the Chrome Browser
  2. Inspect the default Google Search box on chrome browser Reference Image

Refer the image, we have to write the Xpath for Google textarea or search box

driver.findElement(By.Xpath(//input[@id='input'])).sendkeys("testing");

Once we type the text in the search area, google will provide the suggestion based on text, we have to print the console suggested text.

I tried multiple ways to find the XPath but Match element not showing even element presence on webpage.

  1. By.Id('input');
  2. By.Xpath("//input[@id='input']");
  3. By.cssSelector("input#input");
  4. By.Xpath("/html/body/ntp-app//div/div[1]/ntp-realbox//div/inputb");

But Element not finding.


Solution

  • You can't get it, because element is placed in shadow-root, which is placed in shadow-root.

    Reference

    Example of code in Java that reaches your input

    WebElement shadowRoot = driver.findElement(new By.ByTagName("ntp-app")); ;
    JavascriptExecutor js = (JavascriptExecutor) driver;
    WebElement insideShadowInput = (WebElement) js.executeScript("return arguments[0].shadowRoot.querySelector('ntp-realbox').shadowRoot.querySelector('#input');", shadowRoot);
    insideShadowInput.sendKeys("testing");