Natalia
Natalia

Reputation: 3

NoSuchElementError: no such element: Unable to locate element:

I'm trying to type e-mail at the automation test using JS.

var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
until = webdriver.until;

driver.get('http://www.automationpractice.com');
driver.manage().window().maximize();

driver.findElement(webdriver.By.linkText("Sign in")).click();
driver.sleep(10000);

var emailInput = driver.findElement(webdriver.By.id("email_create"));
emailInput.sendKeys("[email protected]");

I get the error UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"css selector","selector":"*[id="email_create"]"} but I'm sure that this selector exists. Could you help?

Upvotes: 0

Views: 1691

Answers (2)

Onlinogame
Onlinogame

Reputation: 1023

The function you're invoking are asynchronous You click and put your driver to sleep, but the rest of the code is still executed.

Try using the await keyword if you're in an async function, or .then() to execute the rest of the code

my_function = async () => {
    var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
    until = webdriver.until;

    await driver.get('http://www.automationpractice.com');
    await driver.manage().window().maximize();

    await driver.findElement(webdriver.By.linkText("Sign in")).click();
    await driver.sleep(10000);

    await driver.findElement(webdriver.By.id("email_create")).sendKeys("[email protected]")
}

Check all the function you're calling to see if it returns a Promise

Upvotes: 3

Sureshmani Kalirajan
Sureshmani Kalirajan

Reputation: 1938

The id is not matching to email input field, try this,

var emailInput = driver.findElement(webdriver.By.id("//input[@name='email']"));
emailInput.sendKeys("[email protected]");

Upvotes: 0

Related Questions