Alexandre Bourlier Alexandre Bourlier - 6 months ago 31
Javascript Question

Selenium - Click on a button until some element appears

I am trying to click on the

Check for new emails
button of the Yopmail's website until an email is received (because at the beginning the inbox is empty).

I am using the
NodeJS
implementation of
Selenium
, running my tests with
Mocha
. Here is how I am trying to click until the element appears :

driver.get('http://yopmail.com');
driver.wait(until.elementLocated(By.css('#f .sbut')));
driver.findElement(By.name('login')).sendKeys(name);
driver.findElement(By.css('#f .sbut')).sendKeys(Key.ENTER);
driver.wait(until.elementLocated(By.id('ifinbox')));// Switching iframe
driver.switchTo().frame("ifinbox");


bool = driver.isElementPresent("m1");


while (!bool) {
driver.switchTo().defaultContent();
driver.findElement(By.id("lrefr")).click();
driver.sleep(500);// 500ms
driver.switchTo().frame("ifinbox");
bool = driver.isElementPresent("m1");
}


The ligne
bool = driver.isElementPresent("m1");
fails with the following unexplicite error message :

Error: the error {} was thrown, throw an Error :)
at Array.forEach (native)


I guess I can't build a while loop on a promise... maybe... But then I dont really understand why it fails, and how to write this while loop properly.

Any suggestion is most welcome!

Answer

According to the source code, isElementPresent() accepts a locator or an element, but you are passing in a string m1. Assuming this is an id:

bool = driver.isElementPresent(By.id("m1"));