C# Selenium SelectElement for dropdown-元素不可交互:元素当前不可见,可能无法操作
这应该是一个非常简单的解决方案,但现在却变得一团糟。刚刚进行了一次大的角度升级(angular 8),由于某些原因,现在没有任何下拉列表可供选择。我过去只需要做一个SendKeys(Keys.Down)并遍历所有选项,直到找到我的选项,但这已经不起作用了 经过一些搜索,我找到了SelectElement方法。下面是我的实现C# Selenium SelectElement for dropdown-元素不可交互:元素当前不可见,可能无法操作,c#,angular,selenium,google-chrome,angular8,C#,Angular,Selenium,Google Chrome,Angular8,这应该是一个非常简单的解决方案,但现在却变得一团糟。刚刚进行了一次大的角度升级(angular 8),由于某些原因,现在没有任何下拉列表可供选择。我过去只需要做一个SendKeys(Keys.Down)并遍历所有选项,直到找到我的选项,但这已经不起作用了 经过一些搜索,我找到了SelectElement方法。下面是我的实现 SelectElement dropdownSelect = new SelectElement(chromeDriver.FindElement(By.CssSelecto
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>
挑选
选择1
选择2
每当我尝试执行这段代码时,我都会遇到这样的错误:“元素不可交互:元素当前不可见,可能无法操作”
它在页面上始终可见,并且可以单击。我在这里绞尽脑汁想如何解决这个问题您可以尝试先单击下拉列表,然后单击所需的选项
chromeDriver.FindElement(By.CssSelector("select[aria-describedby='myfield']")).Click();
chromeDriver.FindElement(By.Xpath("option[@value='option1']")).Click();
您需要等待元素可单击。最有可能的是JavaScript正在做一些事情,selenium的运行速度比JavaScript的执行速度要快
var wait = new WebDriverWait(driver, 10);
wait.Until(d => ExpectedConditions.ElementIsClickable(By.CssSelector("select[aria-describedby='myfield']")));
dropdownSelect.SelectByValue("option1")
你能试试这个答案直到元素可以点击吗?