Javascript 如何使用c检查Selenium中开关切换的状态#

Javascript 如何使用c检查Selenium中开关切换的状态#,javascript,c#,selenium,selenium-chromedriver,specflow,Javascript,C#,Selenium,Selenium Chromedriver,Specflow,我正在尝试使用selenium验证开关是“开”还是“关”,但我似乎无法正确操作。在加载网页时,切换被设置为打开位置,但当它关闭时,过滤器会生成一个表。它工作正常。我只需检查get my selenium tests以获取其当前状态 元素。选定;始终返回false(关于切换位置) 元素。启用;始终返回true(关于切换位置) string test=myToggle.GetAttribute(“类”);返回文本“slider round”,该文本没有帮助 字符串test1=myToggle.Get

我正在尝试使用selenium验证开关是“开”还是“关”,但我似乎无法正确操作。在加载网页时,切换被设置为打开位置,但当它关闭时,过滤器会生成一个表。它工作正常。我只需检查get my selenium tests以获取其当前状态

  • 元素。选定;始终返回false(关于切换位置)
  • 元素。启用;始终返回true(关于切换位置)
  • string test=myToggle.GetAttribute(“类”);返回文本“slider round”,该文本没有帮助
  • 字符串test1=myToggle.GetAttribute(“选中”);是空的
  • 字符串test2=myToggle.GetAttribute(“值”);是空的
  • 代码如下:

    <label class="switch">
    <input type="checkbox" id="myToggle" checked />
    <span class="slider round"></span>
    </label>
    
    我现在的测试是这样的

    使用
    .Selected
    属性应该可以实现您想要做的事情。我之所以添加
    driver.FindElement
    语句是为了确保您找到了正确的元素——根据您的问题描述,我不确定您找到并测试的元素是否正确

    正如另一位用户指出的那样,如果
    GetAttribute(“class”)
    返回
    滑块轮
    ,那么您在这里看到的元素是错误的。确保先找到
    输入
    元素

    文件头
    /*[@id='divTestAcc']/div[1]/p[9]/label/span
    处的XPath将定位
    span
    元素,而不是
    input
    input
    是选中了
    的元素,因此我们需要对其进行测试

    使用
    .Selected
    属性应该可以实现您想要做的事情。我之所以添加
    driver.FindElement
    语句是为了确保您找到了正确的元素——根据您的问题描述,我不确定您找到并测试的元素是否正确

    正如另一位用户指出的那样,如果
    GetAttribute(“class”)
    返回
    滑块轮
    ,那么您在这里看到的元素是错误的。确保先找到
    输入
    元素


    文件头
    /*[@id='divTestAcc']/div[1]/p[9]/label/span
    处的XPath将定位
    span
    元素,而不是
    input
    -
    input
    是选中了
    的元素,因此我们需要对其进行测试。

    如果字符串test=myToggle.GetAttribute(“class”);返回文本“slider round”,然后尝试验证span元素而不是checkbox元素。然后,首先选择正确的元素。如果字符串test=myToggle.GetAttribute(“类”),则所选属性必须正常工作;返回文本“slider round”,然后尝试验证span元素而不是checkbox元素。请先选择正确的元素。所选属性必须正常工作。谢谢!这已经起作用了,我已经坚持了好几天了,但这解决了问题,检查时返回true,未检查时返回false!对非常感谢。这已经起作用了,我已经坚持了好几天了,但这解决了问题,检查时返回true,未检查时返回false!对
       if (Amt == currentAmt ) {
        if ($("#myToggle").prop("checked") == true) {
            loadMyProducts('NAN', $("#total").val());
        }
    
        [FindsBy(How = How.XPath, Using = "//*[@id='divTestAcc']/div[1]/p[9]/label/span")]
      public IWebElement myToggle { get; set; }
    
        public Boolean CheckMyToogleIsOnorOFF()
        {
            string test = myToggle.GetAttribute("class");
            Console.Write("checking "+ test);
    
            // string test1 = myToggle.GetAttribute("checked");
            //string test2 = myToggle.GetProperty("checked");
            // .Selected always returns false
            // .Enables always returns true 
    
            Boolean result;
            result = myToggle.Enabled;
            bool result1 = myToggle.Selected;
    
            if (result == true)
            {
                Console.WriteLine("My toggle is enabled");
            }
            else
            {
                Console.WriteLine("My toggle is not enabled");
            }
            return result;
        }
    
    // is it checked? 
    var isChecked = driver.FindElement(By.XPath("//input[@type='checkbox']").Selected;