-1

I am trying to access user name (id = "username") field and enter data, however I am getting:

element not found exception.

HTML Code:

<form method="post" action="/users/validate" autocomplete="off">
    <input type="hidden" name="authenticity_token" id="authenticity_token" value="ChCVv99e+eLzD4IYQvnyv/tdc9m6yWGi34gSmp6PsbuQIAwIhjhwmDbzsxzXn1GmvpYzA8vpHmkfZNMfIuLNYw==">
    <!-- username non-validatable -->
    <div id="username-form-group" class="form-group label-floating">
        <label for="username" class="control-label">Username</label>
        <input type="text" id="username" name="user[user_name]" class="form-control" autocomplete="off" value="" maxlength="50">
        <small><a class="pull-right" href="/username">Forgot username?</a></small>
    </div>  
</form>

Using Selenium WebDriver and Java.

driver.findElement(By.xpath("//form[@method='post']//input[@id='username']")).sendKeys("ABC");

Looks like not able to switch in the form!

James Z
  • 12,209
  • 10
  • 24
  • 44
Manidipta
  • 1
  • 1

1 Answers1

0

To send a character sequence within the username field, you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("form[action='/users/validate'] label[for='username']"))).sendKeys("Manidipta");
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//form[@action='/users/validate']//label[@for='username']"))).sendKeys("Manidipta");
    

Here you can find a detailed discussion on NoSuchElementException, Selenium unable to locate element

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks @DebanjanB. I did mistake, my page opened in a new tab however i was trying to locate in parent window. By default control stays in parent window, used window handler/iterator to navigate to child window and i was able to successfully enter value in user name field. – Manidipta Aug 22 '19 at 23:00