简体   繁体   English

如何使用Selenium WebDriver和C#单击复选框

[英]How to click the checkbox using Selenium WebDriver and C#

What I'm trying to do is click on (or off) any of the checkboxes on my page. 我想做的是单击(或关闭)页面上的任何复选框。 As you can see from the HTML, each checkbox element would appear to have a unique identifier but when I try to click on it, I get an "Element not found" exception. 从HTML中可以看到,每个复选框元素似乎都具有唯一的标识符,但是当我尝试单击它时,会出现“找不到元素”异常。 I've tried all the standard locators (there's no iframe either) -- I can find the checkbox element, ok but just can't click on it. 我已经尝试了所有标准定位器(也没有iframe)-我可以找到复选框元素,好的,但是无法单击它。

Please view the following HTML: 请查看以下HTML:

<div class="form-horizontal">
    <div class="row">
        <div class="col-sm-3">
            <label for="Url_option1">Option 1</label>
        </div>
        <div class="col-sm-1">
            <input type="checkbox" data-yesno-name="Url_option1" class="url-field-toggle">
            <span class="glyphicon clickable glyphicon-remove" id="toggle_undefined"></span>
            <input id="Url_url_option1" name="Url.url_option1" type="hidden" value="no"> &nbsp;
        </div>
        <div class="col-sm-3">
            <label for="Url_option2">Option 2</label>
        </div>
        <div class="col-sm-1">
            <input type="checkbox" data-yesno-name="Url_option2" class="url-field-toggle">
            <span class="glyphicon clickable glyphicon-remove" id="toggle_undefined"></span>
            <input id="Url_translation_c" name="Url.option2" type="hidden" value="no"> &nbsp;
        </div>
        <div class="col-sm-3">
            <label for="Url_option3">Option 3</label>
        </div>
        <div class="col-sm-1">
            <input type="checkbox" data-yesno-name="Url_option3" class="url-field-toggle">
            <span class="glyphicon clickable glyphicon-remove" id="toggle_undefined"></span>
            <input id="Url_option3" name="Url.option3" type="hidden" value="no"> &nbsp;
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12">
            &nbsp;
        </div>
    </div>
    <div class="row">
        <div class="col-sm-3">
            <label for="Url_option4">Option 4</label>
        </div>
        <div class="col-sm-1">
            <input type="checkbox" data-yesno-name="Url_option4" class="url-field-toggle">
            <span class="glyphicon clickable glyphicon-remove" id="toggle_undefined"></span>
            <input id="Url_option4" name="Url.option4" type="hidden" value="no"> &nbsp;
        </div>
        <div class="col-sm-3">
            <label for="Url_option5">Option 5</label>
        </div>
        <div class="col-sm-1">
            <input type="checkbox" data-yesno-name="Url_option5" class="url-field-toggle">
            <span class="glyphicon clickable glyphicon-remove" id="toggle_undefined"></span>
            <input id="Url_option5" name="Url.option5" type="hidden" value="no"> &nbsp;
        </div>
        <div class="col-sm-3">
            <label for="Url_option6">Option 6</label>
        </div>
        <div class="col-sm-1">
            <input type="checkbox" data-yesno-name="Url_option6" class="url-field-toggle">
            <span class="glyphicon clickable glyphicon-remove" id="toggle_undefined"></span>
            <input id="Url_option6" name="Url.option6" type="hidden" value="no"> &nbsp;
        </div>
    </div>
</div>

The only way I can click on any of the check box elements is by locating the following element: 我可以单击任何复选框元素的唯一方法是找到以下元素:

<span class="glyphicon clickable glyphicon-remove" id="toggle_undefined"></span>  

Using an XPath locator: 使用XPath定位器:

i.e //*[@id="toggle_undefined"]

The click event gets fired in this case but it will obviously only click on the first element (or checkbox in this case) on the page (I've over 20 checkboxes on the page). 在这种情况下,将触发click事件,但显然只会单击页面上的第一个元素(在这种情况下,即复选框)(我在页面上有20个以上的复选框)。

Based on the HTML code above, let's say I wanted to enable the second checkbox element on row two. 根据上面的HTML代码,假设我要在第二行启用第二个复选框元素。 Here is the code for this checkbox element: 这是此复选框元素的代码:

<div class="col-sm-3">
    <label for="Url_option5">Option 5</label>
</div>
<div class="col-sm-1">
    <input type="checkbox" data-yesno-name="Url_option5" class="url-field-toggle">
    <span class="glyphicon clickable glyphicon-remove" id="toggle_undefined"></span>
    <input id="Url_option5" name="Url.option5" type="hidden" value="no"> &nbsp;
</div>

Based on the info above I can only click the checkbox by finding and clicking on the following element: 根据上面的信息,我只能通过找到并单击以下元素来单击复选框:

<span class="glyphicon clickable glyphicon-remove" id="toggle_undefined"></span>

How am I supposed to create a unique XPath locator for this element that I could use to click on any of the checkboxes on my page? 我应该如何为此元素创建一个唯一的XPath定位器,以便单击页面上的任何复选框?

To get input with type checkbox just use css selector below. 要使用类型复选框获得输入,只需使用下面的CSS选择器即可。

Element: 元件:

<input type="checkbox" data-yesno-name="Url_Transaction_Out" class="url-field-toggle" style="" checked="checked"><span class="glyphicon clickable glyphicon-ok" id="toggle_undefined">

Css Selector: CSS选择器:

input[data-yesno-name='Url_Transaction_Out']

Code: 码:

driver.FindElement(By.CssSelector("input[data-yesno-name='Url_Transaction_Out']")).Click();

To check and select: 要检查并选择:

IWebElement checkbox = driver.FindElement(By.CssSelector("input[data-yesno-name='Url_Transaction_Out']"));
if(!checkbox.IsSelected())
{
    checkbox.Click();
}

Use the following CSS Selector for that: 为此,请使用以下CSS选择器:

.url-field-toggle[data-yesno-name='Url_Transaction_Out']

Code is: 代码是:

driver.FindElement(By.CssSelector("url-field-toggle[data-yesno-name='Url_Transaction_Out']")).Click();

Hope it helps you! 希望对您有帮助!

As per the HTML you have shared to invoke click() on the desired checkbox you can use either of the following solutions: 根据您共享的HTML来在所需的复选框上调用click() ,您可以使用以下解决方案之一:

  • CssSelector : CssSelector

     driver.FindElement(By.CssSelector("label[for=Url_Transaction_Out]")).Click(); 
  • XPath : XPath

     driver.FindElement(By.XPath("//label[@for='Url_Transaction_Out']")).Click(); 

Since you need to click the SPAN, eg 由于您需要单击SPAN,例如

<span class="glyphicon clickable glyphicon-remove" id="toggle_undefined"></span>

and there's no unique identifiers/attributes on it, we'll have to locate the right one by using one of the surrounding elements. 并且没有唯一的标识符/属性,我们必须使用周围的元素之一来找到正确的标识符/属性。

Probably what I would use is to find the SPAN by the relative LABEL, eg 大概我会用相对的LABEL查找SPAN,例如

<label for="Url_option1">Option 1</label>

You can use an XPath like 您可以使用类似

//label[.='Option 3']//following::span[@id='toggle_undefined']

What this does is finds the LABEL that contains the desired string, 'Option 3', and then finds the first SPAN that follows with the ID 'toggle_undefined'. 这样做是找到包含所需字符串'Option 3'的LABEL,然后找到ID为'toggle_undefined'的第一个SPAN。

If it were me, I would wrap this in a function and pass in the option name I was looking to click, eg 如果是我,则将其包装在一个函数中,并传递我想要单击的选项名称,例如

public void Toggle(string optionName)
{
    Driver.Value.FindElement(By.XPath($"//label[.='{optionName}']//following::span[@id='toggle_undefined']")).Click();
}

and call it like 并称它为

Toggle("Option 3");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM