How do I verify that an element does not exist in Selenium 2

前端 未结 8 1083
误落风尘
误落风尘 2020-12-05 05:29

In Selenium 2 I want to ensure that an element on the page that the driver has loaded does not exist. I\'m including my naive implementation here.

    WebEle         


        
8条回答
  •  春和景丽
    2020-12-05 05:48

    If you're using the Javascript API, you can use WebElement.findElements(). This method will return a Promise with an array of found elements. You can check the length of the array to ensure no items were found.

    driver.findElements(By.css('.selector')).then(function(elements) {
      expect(elements.length).to.equal(0)
    })
    

    I'm using Chai assertion library inside the Promise's callback to expect a certain value.

    Reference: https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index_exports_WebElement.html

提交回复
热议问题