1

After a page loads the cursor is already in the element I need to enter the text in. How would I just input a string / type without actually having to find the element first since I'm already "active" within it?

1
  • 1
    You should agree the code Commented Jun 26, 2018 at 5:36

3 Answers 3

2

Once the page loads as the cursor is already within the desired element to send a character sequence you can use the following solution:

WebElement myElement = driver.switchTo().activeElement();
myElement.sendKeys("alec kendall");
Sign up to request clarification or add additional context in comments.

Comments

0

Use this - WebElement currentElement = driver.switchTo().activeElement();

Refer to - https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.TargetLocator.html#activeElement--

Comments

0

As DebanjanB writres, you can switch to active element, but if the web behavior changes and cursor will by default elsewhere, the solution won't more work. It is better to find the input field - verified way. An example:

WebDriverWait wait5s = new WebDriverWait(driver,5);
driver.get("https://www.google.cz");

WebElement input_field = wait5s(until(ExpectedConditions.elementToBeClickable(By.id("lst-ib")));
input_field.click(); // some input fields needs to be cliked before sending keys
input_field.sendKeys("egg or chicken");

WebElement search_button = driver.findElement(By.name("btnK"));
search_button.click();

Comments

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.