title: checking bootstrap toggle button state using selenium webdriver
body: <p>I have below html</p>
  
  <pre><code>    &lt;div id="Show"&gt;
         &lt;label style="width: 150px"&gt;Show options&lt;/label&gt;
         &lt;div class="toggle btn btn-default" data-toggle="toggle" data-bind="toggle:mySettings().ShowMe, css:{off:!mySettings().ShowMe()}"&gt;
            &lt;div class="toggle-group"&gt;
                  &lt;label class="btn btn-primary toggle-on"&gt;ON&lt;/label&gt;
                  &lt;label class="btn btn-default active toggle-off"&gt;OFF&lt;/label&gt;
                  &lt;span class="toggle-handle btn btn-default"&gt;&lt;/span&gt;
             &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>I want to see if button is on or off. If it is on then i want to make it off and viceversa. So i tried below code to turn on</p>
  
  <pre><code>var statusOfButton = WebDriver.WaitFor(
              ExpectedConditions.ElementExists(By.XPath("//label[@class='btn btn-primary toggle-on']")),
              TimeSpan.FromSeconds(5));
  
  
  
          if (!statusOfButton.Selected)
          {
             var offButtonElement =  WebDriver.WaitFor(
                  ExpectedConditions.ElementExists(By.XPath("//label[@class='toggle btn btn-default off']")),
                  TimeSpan.FromSeconds(2));
             offButtonElement.Click();
  
  
          }
  </code></pre>
  
  <p>This worked for first instance when test ran. Initially button was off so it made it on. Next time when test ran the button was on and then i got error on this line saying no such element found.</p>
  
  <pre><code>         var offButtonElement =  WebDriver.WaitFor(
                  ExpectedConditions.ElementExists(By.XPath("//label[@class='toggle btn btn-default off']")),
                  TimeSpan.FromSeconds(2));
  </code></pre>
  
  <p>How to fix this?</p>