[英]Selenium C# error: ElementNotIteractableException even though it is DOM clickable
我在 Chrome 上使用 C# Z303CB0EF9EDB9082D61BBBE5825 核心對 bing.com 進行了非常基本的 Selenium 測試。 它是這樣的
[TestMethod]
[TestCategory("Chrome")]
public void TheBingSearchTest2()
{
var appURL = "http://www.bing.com/";
driver.Navigate().GoToUrl(appURL);
driver.Manage().Window.Maximize();
driver.FindElement(By.Id("sb_form_q")).SendKeys("Azure Pipelines");
var koss = driver.FindElement(By.Id("sb_form_go"));
Console.WriteLine("found element {0}", koss.GetAttribute("outerHTML"));
koss.Click();
}
問題是,我不斷收到最后一行的異常(點擊步驟)
消息:測試方法 SeleniumDemo.MySeleniumTests.TheBingSearchTest2 拋出異常:OpenQA.Selenium.ElementNotInteractableException:元素不可交互(會話信息:chrome=105.0.5195.53)
堆棧跟蹤:WebDriver.UnpackAndThrowOnError(Response errorResponse, String commandToExecute) WebDriver.Execute(String driverCommandToExecute, Dictionary`2 參數) WebDriver.InternalExecute(String driverCommandToExecute, Dictionary`2 參數) WebElement.Execute(String commandToExecute, Dictionary`2 參數) WebElement .Click() MySeleniumTests.TheBingSearchTest2() 第 59 行
記錄的 output 是正確的,它給出
found element <input id="sb_form_go" type="submit" aria-label="Search" name="search" value="" tabindex="0">
當做得好 ol' javascript 擺弄時,當我得到元素#sb_form_go 和invoke.checkVisiblity() 它返回true。 我什至可以對其執行.click() 並且它可以工作。
我嘗試在最后一行之前添加等待時間,將方法轉換為異步並執行
await Task.Delay(10000);
不起作用。 我試着做
var wait1 = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait1.Until(ExpectedConditions.ElementToBeClickable(By.Id("sb_form_go")));
但隨后錯誤變為
OpenQA.Selenium.WebDriverTimeoutException:10 秒后超時
這很奇怪,因為我確定它是可見的
這里有什么想法嗎? 這是復制問題的代碼(不是我確切的私人代碼,但行號不同) https://github.com/koko983/SeleniumBingDemo
您似乎正在嘗試<input>
元素,這可能是為其附加了onClick
事件的元素。 在這種情況下 - 有一個<label>
元素,它應該是輸入元素的“包裝器”。
您必須單擊 label 元素才能觸發實際單擊搜索圖標。
我建議,也開始使用wait
功能,作為安全等待 UI 在進行交互之前穩定下來。
例子:
var appURL = "http://www.bing.com/";
driver.Navigate().GoToUrl(appURL);
driver.Manage().Window.Maximize();
// typing inside search box.
driver.FindElement(By.Id("sb_form_q")).SendKeys("Azure Pipelines");
// click on the search icon.
driver.FindElement(By.Id("search_icon")).Click();
只需嘗試單擊父元素:
non_clickable_element.FindElement(By.XPath("..")).click();
或者,嘗試單擊元素的第一個子元素:
non_clickable_element.FindElements(By.XPath(".//*"))[0].click()
大多數情況下,當您無法單擊需要單擊父項或子項的項目時,即使您嘗試單擊的元素可見。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.