无法单击某些复选框硒

本文关键字:复选框 单击 | 更新日期: 2023-09-27 18:35:12

我有一个用户角色列表,我可以在其中选择一些角色。我正在使用开关盒为用户选择选项。问题是我无法仅选中某些复选框。它返回 NoSuchElementException。我尝试调试它并尝试手动单击它,它有效。我无法弄清楚为什么它在同一组复选框下的行为不同。尝试单击span,尝试使用id和XPath,但没有任何效果。我正在附加 HTML、代码和堆栈跟踪。有人可以帮助我解决这个问题吗?

.HTML:

<div>
<span class="checklist">
<span class="">
<input id="SelectedRoleIds4" type="checkbox" value="4" name="SelectedRoleIds"/>
<a class="checkbox-select">Check</a>
<a class="checkbox-deselect">Uncheck</a>
</span>
</span>
<label for="SelectedRoleIds4" style="float: none;">SDA-Basic</label>
</div>
<div>
<span class="checklist">
<span class="">
<input id="SelectedRoleIds5" type="checkbox" value="5" name="SelectedRoleIds"/>
<a class="checkbox-select">Check</a>
<a class="checkbox-deselect">Uncheck</a>
</span>
</span>
<label for="SelectedRoleIds5" style="float: none;">SDA-Manager</label>
</div>
<div>
<span class="checklist">
<span class="">
<input id="SelectedRoleIds6" type="checkbox" value="6" name="SelectedRoleIds"/>
<a class="checkbox-select">Check</a>
<a class="checkbox-deselect">Uncheck</a>
</span>
</span>
<label for="SelectedRoleIds6" style="float: none;">SDA-Admin</label>
</div>
<div>
<span class="checklist">
<span class="">
 <input id="SelectedRoleIds7" type="checkbox" value="7" name="SelectedRoleIds"/>
 <a class="checkbox-select">Check</a>
<a class="checkbox-deselect">Uncheck</a>
</span>
</span>
<label for="SelectedRoleIds7" style="float: none;">SDD-Submit</label>
</div>

法典:

 public static void SetRole(string[] roles)
    {
        foreach (string s in roles)
        {
           // Debug.WriteLine("role:" + s.ToLower());
            switch (s)
            {
                case "sda-basic":
                    if (!Drivers._driverInstance.FindElement(By.Id("SelectedRoleIds4")).Selected)
                        Drivers._driverInstance.FindElement(By.XPath(".//*[@id='tab-1']/fieldset[2]/div[1]/div[1]/span/span/a[1]")).Click();
                    break;
                case "sda-manager":
                    if (!Drivers._driverInstance.FindElement(By.Id("SelectedRoleIds5")).Selected)
                        Drivers._driverInstance.FindElement(By.XPath(".//*[@id='tab-1']/fieldset[2]/div[1]/div[2]/span/span/a[1]")).Click();
                    break;
                case "sda-admin":
                    if (!Drivers._driverInstance.FindElement(By.Id("SelectedRoleIds6")).Selected)
                        Drivers._driverInstance.FindElement(By.XPath(".//*[@id='tab-1']/fieldset[2]/div[1]/div[3]/span/span/a[1]")).Click();
                    break;
                case "sdd-submit":
                    if (!Drivers._driverInstance.FindElement(By.Id("SelectedRoleIds7")).Selected)
                        Drivers._driverInstance.FindElement(By.XPath(".//*[@id='tab-1']/fieldset[2]/div[1]/div[4]/span/span/a[1]")).Click();
                    break;
                case "psl-basic":
                    if (!Drivers._driverInstance.FindElement(By.Id("SelectedRoleIds8")).Selected)
                        Drivers._driverInstance.FindElement(By.XPath(".//*[@id='tab-1']/fieldset[2]/div[1]/div[5]/span/span/a[1]")).Click();
                    break;
                case "psl-admin":
                    if (!Drivers._driverInstance.FindElement(By.Id("SelectedRoleIds9")).Selected)
                        Drivers._driverInstance.FindElement(By.XPath(".//*[@id='tab-1']/fieldset[2]/div[1]/div[6]/span/span/a[1]"));
                        Drivers._driverInstance.FindElement(By.XPath(".//*[@id='tab-1']/fieldset[2]/div[1]/div[6]/span/span/a[1]")).Click();
                    break;
                case "psl-finance":
                    if (!Drivers._driverInstance.FindElement(By.Id("SelectedRoleIds10")).Selected)
                        Drivers._driverInstance.FindElement(By.XPath(".//*[@id='tab-1']/fieldset[2]/div[1]/div[7]/span/span/a[1]")).Click();
                    break;
                case "psl-superadmin":
                    if (!Drivers._driverInstance.FindElement(By.Id("SelectedRoleIds11")).Selected)
                        Drivers._driverInstance.FindElement(By.XPath(".//*[@id='tab-1']/fieldset[2]/div[1]/div[8]/span/span/a[1]")).Click();
                    break;
                case "psl-systems":
                    if (!Drivers._driverInstance.FindElement(By.Id("SelectedRoleIds12")).Selected)
                        Drivers._driverInstance.FindElement(By.XPath(".//*[@id='tab-1']/fieldset[2]/div[1]/div[9]/span/span/a[1]")).Click();
                    break;
                case "sdd-basic":
                    if (!Drivers._driverInstance.FindElement(By.Id("SelectedRoleIds13")).Selected)
                       // Drivers._driverInstance.FindElement(By.XPath(".//*[@id='tab-1']/fieldset[2]/div[1]/div[10]/span/span/a[1]"));
                        Drivers._driverInstance.FindElement(By.XPath(".//*[@id='tab-1']/fieldset[2]/div[1]/div[10]/span/span")).Click();
                    break;
                case "sdd-authorise":
                    if (!Drivers._driverInstance.FindElement(By.Id("SelectedRoleIds14")).Selected)
                        //Drivers._driverInstance.FindElement(By.Id("SelectedRoleIds14")).Click();
                        // Drivers._driverInstance.FindElement(By.XPath(".//*[@id='tab-1']/fieldset[2]/div[1]/div[11]/span/span/a[1]"));
                      //  new WebDriverWait(Drivers._driverInstance, new TimeSpan(0, 0, 10)).Until(ExpectedConditions.ElementIsVisible(By.Id("SelectedRoleIds14")));
                        Drivers._driverInstance.FindElement(By.XPath(".//*[@id='tab-1']/fieldset[2]/div[1]/div[11]/span/span/a[1]")).Click();
                    break;
                case "director":
                    if (!Drivers._driverInstance.FindElement(By.Id("SelectedRoleIds16")).Selected)
                        Drivers._driverInstance.FindElement(By.XPath(".//*[@id='tab-1']/fieldset[2]/div[1]/div[12]/span/span/a[1]")).Click();
                    break;
                case "api user":
                    if (!Drivers._driverInstance.FindElement(By.Id("SelectedRoleIds17")).Selected)
                        Drivers._driverInstance.FindElement(By.XPath(".//*[@id='tab-1']/fieldset[2]/div[1]/div[13]/span/span/a[1]")).Click();
                    break;
                default: throw new Exception("Please select a valid Role for the User");
            }
        }
    }

堆栈跟踪:

Test method SDTestAutomation.Users_Tests.Users_NewUser threw exception: 
OpenQA.Selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[@id='tab-1']/fieldset[2]/div[1]/div[11]/span/span/a[1]"}
Result StackTrace:  
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByXPath(String xpath)
   at OpenQA.Selenium.By.<>c__DisplayClasse.<XPath>b__c(ISearchContext context)
   at OpenQA.Selenium.By.FindElement(ISearchContext context)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
   at SmartDebitTestFramework.Users.SetRole(String[] roles)

我无法选择"sdd-authorise"、"sdd-basic"和另外 2 个。为什么有些找不到?任何帮助将不胜感激。

谢谢

无法单击某些复选框硒

您正在单击跨度。单击输入元素。 我的识别建议是使用 CSS:

Drivers._driverInstance.FindElement(By.CssSelector("input#SelectedRoleIds4")).Click();

或者,如果要专门验证"a"元素是否按预期执行检查单击,则可以执行以下操作: By.XPath("//input[@id="SelectedRoleIds5"]/../a[contains(@class,"-select")]") 请注意,XPath 选择器标识输入字段,备份树一个节点,然后选择在其类属性值中包含"-select"文本的元素。

顺便说一句,我质疑是否需要让"a"元素进行检查和取消检查,当您单击输入元素时,它会自动执行此操作。而且,无论您是在处理代码隐藏还是在测试期间,您始终可以知道何时选择输入元素并采取相应的行动。

问题是用户缺少一些角色和权限。