0
driver.get("<url>")
WebElement name = driver.findElement(By.id(..));
name.click();
name.sendKeys("<input text>");

In the above code is it required to have name.click() first , or is it optional as the field is located and it should be able to send the input without having to click on it first

brlawrence
  • 19
  • 5
  • It depends on the html. From my own experience in some cases `element.sendKeys(String string)` realy does not work without `element.click()`, but in some cases it does. In one project I had to even add `Thread.sleep(100)` after the click. – pburgr Apr 04 '22 at 05:44
  • Did you try that by yourself? Update the question with your observation with both the cases. – cruisepandey Apr 04 '22 at 06:54
  • Thank you . I could not try as Selenium is not working after Chrome driver update. I am going to post this question as a separate post – brlawrence Apr 06 '22 at 02:26

1 Answers1

0

<input> tag

The <input> tag specifies an input field where the user can enter data. The <input> element can be displayed in different ways, depending on the type attribute. As an example:

  • <input type="text"> (default value)
  • <input type="button">
  • <input type="checkbox">
  • <input type="email">
  • <input type="file">
  • <input type="hidden">
  • <input type="number">
  • <input type="password">
  • <input type="radio">
  • <input type="submit">
  • <input type="url">

This usecase

As your usecase involves sending a character sequence presumably it should be of the following type:

<input type="text">

Generally you won't have to invoke click() before invoking sendKeys() but ideally to send a character sequence you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("birtviewer"))).sendKeys("Richard Bernard");
  

However, incase the element is a dynamic element having onfocus event attribute which fires the moment that the element gets focus as follows:

<input type="text" id="fname" onfocus="myFunction(this.id)">

you may require the additional step to click within the element.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352