Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 964
Bob Kimani Avatar asked Oct 16 '25 15:10

Bob Kimani


1 Answers

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);
like image 168
Florent B. Avatar answered Oct 18 '25 05:10

Florent B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!