用于下拉菜单的Selenium SelectElement-元素不可交互:该元素当前不可见,可能无法操作

这应该是一个非常简单的解决方案,但是却变得一团糟。刚进行了一次大的角度升级(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>

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

它始终在页面上可见,并且可以单击。我在这里想办法解决这个问题

megeter 回答:用于下拉菜单的Selenium SelectElement-元素不可交互:该元素当前不可见,可能无法操作

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

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

您需要等待元素可单击。 JavaScript很可能正在做某事,硒的移动速度快于JavaScript的执行速度。

var wait = new WebDriverWait(driver,10);

wait.Until(d => ExpectedConditions.ElementIsClickable(By.CssSelector("select[aria-describedby='myfield']")));

dropdownSelect.SelectByValue("option1")
本文链接:https://www.f2er.com/3165609.html

大家都在问