javascript - How to use Driver Wait Until Text is in selenium - Stack Overflow

I am trying to use driver wait function with the following wait condition.I want to test that the text

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 mands I am using selenium nodeJS implementation with the chrome driver.

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 mands I am using selenium nodeJS implementation with the chrome driver.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Dec 5, 2017 at 10:18 Bob KimaniBob Kimani 1,1641 gold badge22 silver badges40 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745231647a4617703.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信