Check if element exists - selenium / javascript / node-js

后端 未结 6 1432
花落未央
花落未央 2020-12-14 12:21

I\'m trying to check if an element exists before I can execute this line:

driver.findElement(webdriver.By.id(\'test\'));

This throws an error \"

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 13:19

    I know this is a bit late, but this is the code that worked for me.

    var assert = require('assert');
    var expect = require('chai').expect;
    var should = require('chai').should();
    var chai = require('chai');
    var chaiAsPromised = require('chai-as-promised');
    
    chai.use(chaiAsPromised);
    chai.should();
    
    var webdriver = require('selenium-webdriver');
    By = webdriver.By;
    until = webdriver.until;
    
    describe('checking if an element id exists', function() {
    
      it.only('element id exists', function () {
        var driver = new webdriver.Builder().forBrowser('chrome').build();
        driver.get('http://localhost:3000');
        this.timeout(6000);
    
        return driver.wait(until.elementLocated(By.id('idWeWantToTest')), 5 * 1000).then( (e) => {
        }, function(err) {
          throw {
            msg: "element not found"
          }
        }).then( () => {
          driver.quit();
        });     
      });
    })
    

提交回复
热议问题