【问题标题】:Selenium SelectElement for dropdown - element not interactable: Element is not currently visible and may not be manipulated用于下拉菜单的 Selenium SelectElement - 元素不可交互:元素当前不可见且可能无法操作
【发布时间】:2019-11-04 15:27:59
【问题描述】:

这应该是一个非常简单的解决方案,但现在变得一团糟。刚刚进行了一次大的角度升级(Angular 8),现在由于某种原因不再可以选择任何下拉菜单。我曾经能够只做一个 SendKeys(Keys.Down) 并遍历所有选项,直到找到我的选项,但这不再起作用了。

经过一番搜索,我找到了 SelectElement 方法。下面是我的实现。

SelectElement dropdownSelect = new SelectElement(chromeDriver.FindElement(By.CssSelector("select[aria-describedby='myfield']")));

dropdownSelect.SelectByValue("option1");

html 是这样的

<select _ngcontent-teu-c24="" aria-describedby="myfield">
    <option _ngcontent-teu-c24="" disabled="" value="" ng-reflect-value="">Select</option>
    <option _ngcontent-teu-c24="" value="option1" ng-reflect-value="option1" class="ng-star-inserted">option1</option>
    <option _ngcontent-teu-c24="" value="option2" ng-reflect-value="option2" class="ng-star-inserted">option2</option>
</select>

每当我尝试执行此代码时,我都会收到此错误:“元素不可交互:元素当前不可见并且可能无法操作”

它在页面上始终可见并且可以点击。我正在为如何解决这个问题而摸不着头脑

【问题讨论】:

标签: c# angular selenium google-chrome angular8


【解决方案1】:

您可以尝试先单击下拉菜单,然后单击所需的选项。

   chromeDriver.FindElement(By.CssSelector("select[aria-describedby='myfield']")).Click();
   chromeDriver.FindElement(By.Xpath("option[@value='option1']")).Click();

【讨论】:

    【解决方案2】:

    您需要等待元素可点击。很可能 JavaScript 正在做某事,而 selenium 的运行速度比 JavaScript 正在执行的要快。

    var wait = new WebDriverWait(driver, 10);
    
    wait.Until(d => ExpectedConditions.ElementIsClickable(By.CssSelector("select[aria-describedby='myfield']")));
    
    dropdownSelect.SelectByValue("option1")
    

    【讨论】:

      猜你喜欢
      • 2017-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2017-01-27
      • 1970-01-01
      • 2018-09-17
      相关资源
      最近更新 更多