【问题标题】:Selenium: Dropdown cannot be clicked as it is obscured by buttonSelenium:无法单击下拉菜单,因为它被按钮遮挡
【发布时间】:2018-06-26 15:25:19
【问题描述】:

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

结果消息: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; }

这是 selenium 尝试选择选择时下拉列表的样子。没有什么东西能挡住我的视线。

【问题讨论】:

  • 你在最大化你的浏览器吗?

标签: c# selenium dropdown


【解决方案1】:

好的,这里有几个选项

  1. 检查您是否正在最大化窗口 -

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

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

【讨论】:

  • 选项 2 怎么样?
  • 它总是在 selectbytext 行
  • @johnc489- 你试过按索引选择,按值选择吗?
  • 啊酷,您可以将其标记为将来参考的答案
  • 当然完成,也只是注意到这只发生在 Firefox 上。谷歌浏览器可以使用 selectby。
【解决方案2】:

XPath 需要修改如下

//select[@id='year']

否则,

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

【讨论】:

    【解决方案3】:

    试试这个代码:

    var down= driver.FindElement(By.id("year"));
    var DropDown = new SelectElement(down);  
    
    //select by value
    DropDown .SelectByValue("2017"); 
     // select by text
     DropDown .SelectByText("2017");  
    

    你可以有这样的网络元素:

     [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();
        }
        }
    

    【讨论】:

    • 不,显然仍然被按钮遮挡。还是谢谢
    • 当您运行您的脚本时,您是否能够看到下拉菜单被点击,或者您必须进行某种手动干预才能看到这种情况发生?
    • 如果这就是你的意思,我可以看到 firefox 窗口。
    • 是的,您可以看到 Firefox 窗口,您必须在其中看到您的脚本正在运行。因此,当您的脚本尝试单击下拉菜单并想要选择某个值时,您是否会做任何事情,是否需要手动干预?
    • 不,通常我只是让脚本运行,无需人工干预。这段代码曾经可以工作,但我猜前端开发人员改变了一些东西。
    【解决方案4】:

    根据异常类型,可以通过以下点击方法之一解决:

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

    JavascriptExecutor js = (JavascriptExecutor) driver;  
    js.executeScript("arguments[0].click();","Target Webelement");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-18
      相关资源
      最近更新 更多