Bob Kimani
Bob Kimani

Reputation: 1164

How to use Driver Wait Until Text is in selenium

I am trying to use driver wait function with the following wait condition.

I want to test that the text on a button is equal to/matches "Sign Up". The following is my code:

driver.wait(until.elementTextIs(By.css('body > div.site-wrapper > div > div 
> div.inner.cover > p:nth-child(3) > a.btn.btn-lg.btn-primary'),'Sign 
Up'),80000)

But after running it I get the error:

C:\Users\bob\Documents\testElectron\node_modules\selenium-
webdriver\lib\promise.js:2626   Uncaught TypeError: element.getText is not a 
function

I have tried retrieving the text on the button manually using

var Button = driver.findElement(By.css('body > div.site-wrapper > div > div 
> div.inner.cover > p:nth-child(3) > a.btn.btn-lg.btn-primary'));
Button.getText().then(function(text){
 console.log(text);
});

and it works , but I would like to use the condition for the wait. PS: The button does exist and is visible when I run the commands I am using selenium nodeJS implementation with the chrome driver.

Upvotes: 4

Views: 4402

Answers (1)

Florent B.
Florent B.

Reputation: 42538

The function until.elementTextIs takes a web element but you are providing a locator.

Either wait for the element and then for the text:

var buttonLogin = By.css('body > div.site-wrapper > div > div > div.inner.cover > p:nth-child(3) > a.btn.btn-lg.btn-primary');

driver.wait(until.elementTextIs(driver.wait(until.elementLocated(buttonLogin)), 'Sign Up'), 80000);

or create an expected condition which will wait for an element that has the desired text:

var Condition = webdriver.Condition;

until.elementLocatedTextIs = function elementLocatedTextIs(locator, text) {
  return new Condition(
    'for element to be located ' + locator + ' with text ' + text,
    function(driver) {
      return driver.findElements(locator).then(function(elements) {
        return elements.filter(function(element) {
          return element.getText().then(t => t === text ? element : null);
        }).then(function(elements) {
          return elements[0];
        });
      });
    });
};

var buttonLogin = By.css('body > div.site-wrapper > div > div > div.inner.cover > p:nth-child(3) > a.btn.btn-lg.btn-primary');

driver.wait(until.elementLocatedTextIs(buttonLogin), 'Sign Up'), 80000);

Upvotes: 6

Related Questions