如何在Selenium中验证toggle是开启还是关闭?

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

我在selenium + pure java中面临一个问题。我在屏幕上有这个toggle,我想知道它什么时候是活动的。enter image description hereenter image description here

这是激活时的elemt代码。enter image description here

而这是不激活时的代码enter image description here

区别在于button的类。

我写了这段代码。

public Boolean isDealActive() throws Exception
    {
       String test1 =  getWebDriver().findElement(By.xpath("//*[@id='switch_deal_status_toggle']//button")).getAttribute("checked");
       Boolean test2 = getWebDriver().findElement(By.xpath("//*[@id='switch_deal_status_toggle']//button")).isSelected();
       String test3 =  getWebDriver().findElement(By.xpath("//*[@id='switch_deal_status_toggle']")).getAttribute("checked");
       Boolean test4 = getWebDriver().findElement(By.xpath("//*[@id='switch_deal_status_toggle']")).isSelected();
        return  isElementSelected(DEAL_STATUS_XPATH);
    }

当我使用get属性时,结果是nullwhen我使用is selected时,结果是false和not true.the toggle now is ON,我只是得到错误的结果can someone advise? **不要注意return语句,在得到selenium的正确答案后,我将会修正它

angularjs selenium selenium-webdriver selenium-chromedriver
1个回答
0
投票

试试下面的实现

WebElement toggleBtn = driver.findElement(By.xpath("//*[@id='switch_deal_status_toggle']/button"));
String classAttributes = toggleBtn.getAttribute("class");
if (classAttributes.contains("ant-switch-checked")) {
    // checked
} else {
    // not checked
}
© www.soinside.com 2019 - 2024. All rights reserved.