无法点击某些复选框selenium

时间:2016-02-17 14:42:05

标签: c# selenium selenium-webdriver

我有一个用户角色列表,我可以在其中选择一些角色。我使用开关盒为用户选择选项。问题是我无法仅查看某些复选框。它返回NoSuchElementException。我尝试调试它并试图手动点击它,它工作。我无法弄清楚为什么它在同一组复选框中表现不同。尝试点击跨度,尝试使用id和XPath但没有任何效果。我附加了HTML,代码和堆栈跟踪。有人可以帮我解决这个问题。

HTML:

Task users = this.GetUsers();
Task visitors = this.GetFrequentVisitors();
Task settings = this.GetTenantSettings();
Task translations = this.GetTranslations();

await Task.WhenAll(users, visitors, settings, translations);

代码:

<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");
            }
        }

    }

我无法选择&#39; sdd-authorize&#39; sdd-basic&#39;还有2个。为什么有些人无法找到?任何帮助将不胜感激。

由于

2 个答案:

答案 0 :(得分:0)

您点击了跨度。单击input元素。我建议识别是使用CSS:

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

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

顺便说一句,我质疑是否需要使用“a”元素进行检查,并在输入元素单击时自动执行此操作时取消选中。而且,无论您是在处理代码隐藏还是在测试期间,您都可以随时了解输入元素何时被选中并采取相应的行动。

答案 1 :(得分:0)

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