0

I was dealing with a search input box icon which is clicked to open the input box. Since the icon is hidden I used JavaScriptExecutor to click on it and open the search input, like

WebElement searchBtn = driver.findElement(By.className("search-toggle"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript ("arguments[0].click();" , searchBtn);

But now, I need to enter some text into the input and ENTER to submit it. The solution would be to use the JavascriptExecutor again, like

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById(**'gbqfq'**).value = 'search text';");

This time, the elementId 'gbqfq' is un known, right ? How do I go around this problem?

2
  • 1
    Please take a moment to read: stackoverflow.com/help/mcve This problem can't be answered without knowing the rendered HTML you are working with. Commented Jul 18, 2017 at 20:31
  • 1
    Why would you need to send text into a hidden element? A user can't interact with a hidden element. Walk through the manual steps on the site to accomplish your task and then automate what you did manually. Commented Jul 18, 2017 at 23:00

1 Answer 1

1

If you don't have the id then use another selector like By.Class or By.Xpath to find the element in selenium and use SendKeys to set the text or the equivalent JavaScript methods using .querySelector to find the element and using the value property to to set the text.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for reply. If you see the example code I gave at the top, it is javascript syntax I needed to follow "document.getElementById('gbqfq').value = 'search text';"), not the Selenium/Java one.
@pranuta99 this answer does address the syntax you want unless I'm missing something... jse.executeScript("document.querySelector(".your > .css > .selector).value = 'search text';")
You are saying the ID is unknown. So you three options 1) Parse the id. But to parse the ID you'll need to find the button by some other selector in the first place and if that's the case you can click it directly. 2) find it by some unique attribute like name but then you'll need an index so this is equivalent to the lat option , 3) Find the element by an xpath or css selector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.