2

The problem is that I cannot find and enter the text in the fields

<input onkeyup="$('#firstname').val(this.value);" type="text" class="is_required validate form-control" data-validate="isName" id="customer_firstname" name="customer_firstname" value="" xpath="1">

I tried using id and Xpath but it doesn't work

driver.findElement(By.id("customer_firstname")).sendKeys("Mario");

Or by Xpath

driver.findElement(By.xpath("//input[@id='customer_firstname']")).sendKeys("Mario");

Any tips? :)

This is the website. Insert email -> click 'create an account' -> and there are those fields to fill in to register.

1
  • Do you get any exception or just sent keys do not appear in text box?
    – Alexey R.
    Commented Jan 14, 2019 at 9:22

2 Answers 2

2

Wait for the element before trying to write on it. You can implement the waiting both implicitly and explicitly. The fluent wait is another option.

Implicit wait:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Wait for maximum 10 seconds 
driver.findElement(By.id("customer_firstname")).sendKeys("Mario");

Explicit wait:

Wait for the element with a specific condition. Here we can wait for the element until the element is visible.

WebDriverWait wait = new WebDriverWait(driver, 10); // Wait for maximum 10 seconds
WebElement firstName = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("customer_firstname")));
firstName.sendKeys("Mario");

Fluent wait:

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    .withTimeout(10, TimeUnit.SECONDS) // Waiting maximum 10 seconds for presence of the element on the page
    .pollingEvery(2, TimeUnit.SECONDS) // Checking for presence in every 2 seconds.
    .ignoring(NoSuchElementException.class);

WebElement firstName = wait.until(new Function<WebDriver, WebElement>() 
{
  public WebElement apply(WebDriver driver) {
      return driver.findElement(By.id("customer_firstname"));
  }
});

firstName.sendKeys("Mario");
1

Your Xpath expression seems to be fine, may be you need to wait before finding and sending keys into that textbox.

Although Thread.sleep(5000) is not recommended but you could try it to test if that's the problem. or try below.

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement textBoxCustomer_Name =  wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("customer_firstname")));
textBoxCustomer_Name.sendKeys("Mario");

It will wait till WebElement is loaded inside DOM for 10 seconds at max & will continue once found, before throwing NoSuchElementException.

Hope this helps.

1
  • Thx for response I will check the 'fix' this evening :)
    – Morris
    Commented Jan 14, 2019 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.