In my current project, I encountered a search input box icon that needed to be clicked in order to open the input box. The issue was that the icon was hidden, so I decided to utilize JavaScriptExecutor to programmatically click on it and reveal the search input field:
WebElement searchBtn = driver.findElement(By.className("search-toggle"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", searchBtn);
However, my next challenge was to enter text into the input field and then submit the search query by pressing ENTER key. To achieve this, I planned to once again use JavascriptExecutor as follows:
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('gbqfq').value = 'search text';");
The problem now is that the elementId 'gbqfq' is not known beforehand. How can I work around this particular issue?