我无法根据其现有状态自动切换按钮

问题描述 投票:0回答:1

在我的情况下,我想首先检查切换按钮是否打开/关闭。如果它关闭然后我想打开它,如果它已经打开然后它应该继续。我使用了isSelected,isEnabled和isDisplayed,但是这个方法都没有讨论按钮的状态。

**Following is my trial code:**

if(driver.findElement(By.xpath("(//span[@class='toggle-handle btn btn-default'])[2]")).isSelected()) {
System.out.println("is turned on");
}
else {
driver.findElement(By.xpath("(//div[@class='feed-toggle-container brand-integrity-site-visibility'])[2]")).click();
}

HTML:

<tr>
    <td>Atlas Copco Brand Integrity Site</td>
    <td>ac.website.com</td>
    <td>
        <div class="feed-toggle-container brand-integrity-site-visibility">
            <div class="toggle btn btn-default off" data-toggle="toggle" style="width: 73px; height: 32px;">
                <input class="feed-toggle" data-url="https://admin.stage.cwsplatform.com/api/brand-integrity-site/7" data-toggle="toggle" data-onstyle="success" name="visible_to_dealer" type="checkbox" value="1">
                <div class="toggle-group">
                    <label class="btn btn-success toggle-on">On</label>
                    <label class="btn btn-default active toggle-off">Off</label><span class="toggle-handle btn btn-default"></span>
                </div>
            </div>
        </div>
    </td>
    <td>
        <a target="_blank" href="http://ac.website.com.live.stage.cwsplatform.com/cws-login">
            <button class="btn btn-primary">Manage Content</button>
        </a>
    </td>
    <td>
        <a target="_blank" href="http://ac.website.com.demo.stage.cwsplatform.com/cws-login">
            <button class="btn btn-default">Manage Content</button>
        </a>
    </td>
</tr>

feed-toggle-container品牌完整性 - 网站 - 可见性。单击此类基本上可以打开和关闭按钮。如果它在它上面会关闭它,反之亦然。使用上面的代码只是关闭On状态而不是将其保持为on状态。

按钮打开时的HTML:

<label class="btn btn-success toggle-on">On</label>

按钮关闭时的HTML:

<label class="btn btn-default active toggle-off">Off</label>
selenium-webdriver toggle ui-automation
1个回答
0
投票

您可以执行以下方案:

  • 您需要先检查其当前状态
  • 您可以根据班级值进行检查
  • 获得类值后,您可以检查并验证其状态
  • 因此您可以处理用户定义的操作

//This is to Locate on label
By locateOnLabel = By.xpath("//div[@class='toggle-group']//label[contains(text(),'On')]");


//This is to Locate off label    
By locateOffLabel = By.xpath("//div[@class='toggle-group']//label[contains(text(),'Off')]");

//Retrieve their class, it given identification whether its On/Off
String getLabelONClass = driver.findElement(locateOnLabel).getAttribute("class");
String getLabelOFFClass = driver.findElement(locateOffLabel).getAttribute("class");

//compare class values, and Perform actions
if(getLabelOFFClass.equals("btn btn-default active toggle-off)) {      
       Click that Webelement if you want to turn on 
}
   else if (getLabelONClass.equals("btn btn-success toggle-on")) {                   
       You can notify message, That Button is already ON
  }
© www.soinside.com 2019 - 2024. All rights reserved.