硒:无法单击下拉列表,因为它被按钮遮住了

时间:2018-06-26 15:25:19

标签: c# selenium dropdown

我有一些测试用例,它们会在下拉列表中选择每个选项,但是无论我目前做什么,都会遇到此错误。

结果消息:System.InvalidOperationException:元素在点(1170.0333251953125,405.4250030517578)不可点击,因为另一个元素将其遮盖了

<div class="btn-group bootstrap-select">
    <button type="button" class="btn dropdown-toggle btn-default" data-toggle="dropdown" role="button" data-id="year" title="2018" aria-expanded="false">
        <span class="filter-option pull-left">2018</span>&nbsp;
        <span class="bs-caret">
            <span class="caret" />
        </span>
    </button>
    <div class="dropdown-menu open" role="combobox" style="max-height: 272px; overflow: hidden; min-height: 0px;">
        <ul class="dropdown-menu inner" role="listbox" aria-expanded="false" style="max-height: 260px; overflow-y: auto; min-height: 0px;">
            <li data-original-index="0" class="selected">
                <a tabindex="0" class="" data-tokens="null" role="option" aria-disabled="false" aria-selected="true">
                    <span class="text">2018</span><span class="glyphicon glyphicon-ok check-mark" />
                </a>
            </li>
            <li data-original-index="1">
                <a tabindex="0" class="" data-tokens="null" role="option" aria-disabled="false" aria-selected="false">
                    <span class="text">2017</span><span class="glyphicon glyphicon-ok check-mark" />
                </a>
            </li>
        </ul>
    </div>
    <select aria-label="view all previous payments" class="selectpicker" data-val="true" data-val-number="The field Year must be a number." data-val-required="The Year field is required."  id="year" name="Year" tabindex="-98">
        <option selected="selected" value="2018">2018</option>
        <option value="2017">2017</option>
    </select>
</div>

当前正在尝试使用此代码更改年份-

 SelectElement DropDown = new SelectElement(ObjectIdentification);
 DropDown.SelectByValue(ValueToBeSelected);
 return true;

通过这样定义WebElement-

 [FindsBy(How = How.XPath, Using = "//*[contains(@class, 'selectpicker')]")]
 private IWebElement DropDownYear { get; set; }

enter image description here

这是硒尝试选择拾取时下拉菜单的外观。我什么都看不见。

4 个答案:

答案 0 :(得分:0)

需要如下更改XPath

//select[@id='year']

否则,

使用ID或名称定位器选择元素

答案 1 :(得分:0)

尝试此代码:

var down= driver.FindElement(By.id("year"));
var DropDown = new SelectElement(down);  

//select by value
DropDown .SelectByValue("2017"); 
 // select by text
 DropDown .SelectByText("2017");  

您可以具有如下Web元素:

 [FindsBy(How = How.id, Using = "year")]
 private IWebElement DropDownYear { get; set; }  

更新:

IList<IWebElement> options= webDriver.FindElements(By.CssSelector("select[id='year']>option"));  
foreach (IWebElement element in options){  
     if(element.GetText().Equals("2017")){  

        element.Click();
    }
    }

答案 2 :(得分:0)

好,这是几个选择

  1. 检查是否要最大化窗口-

     driver.Manage().Window.Maximize();
    

    OR

  2. 使用动作类来定位和动作元素

    IWebElement element = driver.FindElement(By.Id("year"));
    Actions action = new Actions(driver);
    action.MoveToElement(element);
    var DropDown = new SelectElement(element);
    DropDown.SelectByText("2017");
    

更新

  1. IJavscript执行器

    IWebElement element = driver.FindElement(By.Id("year"));
    ((IJavaScriptExecutor)driver).ExecuteScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", element, "2017");
    

答案 3 :(得分:0)

基于异常类型,可以使用以下单击方法之一来解决它:

active

OR

Actions builder = new Actions(driver);
builder.MoveToElement("Your target element").click().Perform();