1
<input name="txtAnswer" class="box1" id="txtAnswer" type="text" maxlength="20">

My code:

driver.findElement(By.name("txtAnswer")).sendKeys("green");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Ali Sultan
  • 13
  • 6
  • formatting required to make this more readable – Akber Iqbal Apr 16 '19 at 06:28
  • First of all please provide us more information, there is no error message in this question and before sendKey() method try to click that element. Try sendKeys after clicking. – sayhan Apr 16 '19 at 07:46

2 Answers2

0

To locate the desired element you can use either of the following Locator Strategies:

  • Using cssSelector:

    driver.findElement(By.cssSelector("input#txtAnswer[class^='box'][name='txtAnswer']")).sendKeys("green")
    
  • Using xpath:

    driver.findElement(By.xpath("//input[starts-with(@class, 'box') and @id='txtAnswer'][@name='txtAnswer']")).sendKeys("green")
    

Update

As you are seeing the error Unable to locate element you need to induce WebDruverWait for the element to be clickable and you can use either of the following solutions:

  • Using cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#txtAnswer[class^='box'][name='txtAnswer']"))).sendKeys("green")
    
  • Using xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[starts-with(@class, 'box') and @id='txtAnswer'][@name='txtAnswer']"))).sendKeys("green")
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • thanks for replay, but it also view error message Unable to locate element: {"method":"xpath","selector":"//input[starts-with(@class, 'box') and @id='txtAnswer'][@name='txtAnswer']"} (Session info: chrome=73.0.3683.86) – Ali Sultan Apr 16 '19 at 10:14
  • thanks for you support, but also get same error (Expected condition failed: waiting for element to be clickable: By.cssSelector: input#txtAnswer[class^='box'][name='txtAnswer'] (tried for 20 second(s) with 500 milliseconds interval) at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95) at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272) at Demo.TasheelIE.main(TasheelIE.java:46) Caused by: org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == input#txtAnswer[class^='box'][name='txtAnswer']) – Ali Sultan Apr 17 '19 at 05:40
0

Id and class are two attributes for an web-element. to identify web element uniquely(one) id is used and to identify web elements use common properties like class, tag name etc.. so writing xpath with id attribute will give you unique match.

Answer: driver.findElement(By.id("txtAnswer")).sendKeys("green");

vijay
  • 1
  • 2