How to send text to the search field through Selenium Webdriver?

13,791

Use below xpath :

(//input[@id='search-text'])[2]

and use like :

driver.findElement(By.xpath("(//input[@id='search-text'])[2]")).sendKeys("FAA");

When you find by this id in console it is giving two elements and first one is not visible but second one is the actual input box.

Share:
13,791
Kunal Soni
Author by

Kunal Soni

Updated on June 04, 2022

Comments

  • Kunal Soni
    Kunal Soni almost 2 years

    Task: search FAA in search box : enter image description here

    I have tried this:-

    webdriver.select_tabs(search.btnSearch);
    
    Thread.sleep(3000);
    WebElement searchbox = driver.findElement(By.id("search-text"));
    Actions builder = new Actions(driver);
    Actions seriesOfActions = builder.moveToElement(searchbox).click().sendKeys(searchbox, "FAA");
    seriesOfActions.perform();
    
    WebDriverWait wait = new WebDriverWait(driver, 30);
    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"search-text\"]")));
    element.sendKeys("FAA");
    element.sendKeys(Keys.ENTER);
    
    webdriver.enter_key(search.txtSearch, Keys.ENTER);
    webdriver.enter_Text(search.txtSearch, "FAA");
    webdriver.enter_key(search.txtSearch, Keys.ENTER);
    

    Got this error:-

    org.openqa.selenium.ElementNotVisibleException: element not visible
    
    • Kovacic
      Kovacic almost 6 years
      please provide HTML source.