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?
3 Answers
Use this - WebElement currentElement = driver.switchTo().activeElement();
Comments
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();