How To Test for Disabled Elements in Selenium Java, Python, Ruby, C#

Yosuva ArulanthuC#, Java, Python, Ruby, Selenium, Test Automation, TestingLeave a Comment

On occasion, you may have the need to check if an element on a page is disabled or enabled.

If we look at the API documentation for Selenium’s IWebElement Interface, we can see there is an available method called Enabled that can help us accomplish what we want.

Let’s take a look at how to use it.

Example

For this example, we will use the dropdown list from the-internet. This list has a few options to select, one of which should be disabled. Let’s find this element and assert that it is in fact disabled.

Java

// filename: DisabledElements.java
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

public class DisabledElements {
    WebDriver driver;

    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();

    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }
    @Test
    public void test() {
        driver.get("http://the-internet.herokuapp.com/dropdown");
        Select dropdown = new Select(driver.findElement(By.id("dropdown")));
        assertThat(dropdown.getOptions().get(0).isEnabled(), is(false));
    }

}

Python

# filename: disabled_elements.py
import unittest
from selenium import webdriver


class DisabledElements(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def tearDown(self):
        self.driver.quit()
    def test_dropdown(self):
        driver = self.driver
        driver.get('http://the-internet.herokuapp.com/dropdown')
        dropdown_list = driver.find_elements_by_tag_name('option')
        assert dropdown_list[0].is_enabled() is False

if __name__ == "__main__":
    unittest.main()

Ruby

# filename: dropdown_disabled.rb

require 'selenium-webdriver'
require 'rspec/expectations'

include RSpec::Matchers

def setup
  @driver = Selenium::WebDriver.for :firefox
end

def teardown
  @driver.quit
end

def run
  setup
  yield
  teardown
end
run do
  @driver.get 'http://the-internet.herokuapp.com/dropdown'
  dropdowns = @driver.find_elements(tag_name: 'option')
  item_of_interest = dropdowns.find { |dropdown| dropdown.text == 'Please select an option' }
  expect(item_of_interest.enabled?).to eql false
end

C#

// filename: DisabledElements.cs
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

public class DisabledElements
{
    IWebDriver Driver;

    [SetUp]
    public void SetUp()
    {
        Driver = new FirefoxDriver();
    }

    [TearDown]
    public void TearDown()
    {
        Driver.Quit();
    }
    [Test]
    public void ElementDisabled()
    {
        Driver.Navigate().GoToUrl("http://the-internet.herokuapp.com/dropdown");
        var Dropdown = new SelectElement(Driver.FindElement(By.Id("dropdown")));
        // The SelectedOption we want is selected by default on page load
        Assert.False(Dropdown.SelectedOption.Enabled);
    }
}

When you save this file and run it ,here is what will happen:

  • Open a browser
  • Visit the page
  • Grab the dropdown list
  • Assert that the target element is not enabled
  • Close the browser

Leave a Reply

Your email address will not be published.