Search code examples
selenium-webdriverxpathwebdriverwebdriverwait

How to locate the search box on Google Home page using Xpath


I have entered correct from the developer tool xpath still it is not macthing element. How can find the xpath or other locator for google search box.

HTML:

<input id="input" type="search" autocomplete="off" spellcheck="false" role="combobox" ariacontrols="matches" placeholder="Search Google or type a URL" aria-expanded="false" aria-live="polite">

Relative Xpath:

/html/body/ntp-app/div/div[2]/ntp-realbox/div/input

Absolute Xpath:

//*[@id="input"]

Snapshot A: enter image description here

Snapshot B:

enter image description here


Solution

  • Given the snapshot:

    snapshot

    You are trying to locate the search box of the default chrome browser page opened manaually, which won't be available within Selenium driven ChromeDriver initiated Browsing Context


    Ideally you should access an url. If you access the url https://www.google.com/ you can locate the search box using the following locator strategy:

    • Code block:

      driver.get("https://www.google.com/")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("BRAJESH KUMAR")
      
    • Browser snapshot:

    BRAJESH KUMAR