Kunal Soni
Kunal Soni

Reputation: 23

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

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

Upvotes: 2

Views: 18393

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193078

To send a character sequence to the search field on the website https://faatoday.com/ you need to induce WebDriverwait to wait for the search icon to be clickable and then again induce WebDriverWait once again for the desired element to be clickable and then send the character sequence as follows:

  • Code Block:

    driver.get("https://faatoday.com/");
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#navbarNav span.sicon-holder.fabutton#searchicon>i.fa.ssearch.fa-search.fa-lg#sicons"))).click();
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='navbarNav']//input[@class='search-text form-control mr-sm-2' and @id='search-text']"))).sendKeys("FAA");
    
  • Browser Snapshot:

faatoday_search

Upvotes: 0

dangi13
dangi13

Reputation: 1275

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.

Upvotes: 1

sagarwadhwa1
sagarwadhwa1

Reputation: 942

By definition, Selenium interacts with the browser like a real user would. A real user would not be able to type into a textbox/editbox which is hidden. Either you need to change the visibility of the input, re-evaluate why you need to interact with a hidden element, or use javascript executor to set the value of the input, something like this:

driver.executeScript("arguments[0].value='" + textToEnter + "'", element);

Upvotes: 0

Related Questions