最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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);
发布评论

评论列表(0)

  1. 暂无评论