0

HTML:

<input type="text" placeholder="Enter name" class="form-control m-2">

I am trying to send text to this through Selenium. This is what I tried:

driver.findElement(By.cssSelector("input.form-control m-2[placeholder='Enter name']")).sendKeys("Test");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

3 Answers3

0

form-control and m-2 are two different classes.

The selector should be input.form-control.m-2[placeholder='Enter name']

Shuo
  • 1,512
  • 1
  • 3
  • 13
0

Use xpath to interact with the web Element. The xpath will be -

//input[@placeholder='Enter name'][@type='text']

Use the following code -

WebElement inputName = driver.findElement(By.xpath("//input[@placeholder='Enter name'][@type='text']"));
inputName.sendKeys("Test");
indayush
  • 55
  • 2
  • 9
0

To send a character sequence to the desired element with placeholder as Enter name you can use either of the following Locator Strategies:

  • cssSelector:

    driver.findElement(By.cssSelector("input[placeholder='Enter name']")).sendKeys("Test");
    
  • xpath:

    driver.findElement(By.xpath("//input[@placeholder='Enter name']")).sendKeys("Test");
    

Ideally, to send a character sequence within the <input> element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:

  • cssSelector:

    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[placeholder='Enter name']"))).sendKeys("Test");
    
  • xpath:

    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@placeholder='Enter name']"))).sendKeys("Test");
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352