简体   繁体   English

ElementNotInteractableException:元素不可交互 C# Nunit Selenium

[英]ElementNotInteractableException : element not interactable C# Nunit Selenium

I am new to Selenium C# Nunit.我是 Selenium C# Nunit 的新手。 I ran following line of codes我运行了以下代码行

 IWebElement SplitCase = driver.FindElement(By.XPath(".//*[@id='OpportunityPageV2UsrSplitCase503e4272-cdbd-44d2-98c2-e67a2996c717ComboBoxEdit-el']"));
 SplitCase.Click();
        
 IWebElement SplitCaseYes = driver.FindElement(By.CssSelector("li[data-item-marker=Yes]"));

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30)); var wait = new WebDriverWait(驱动程序,TimeSpan.FromSeconds(30)); wait.Until(d => (bool)(d as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0")); wait.Until(d => (bool)(d as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0"));

 SplitCaseYes.Click();

I got following Message: Message: OpenQA.Selenium.ElementNotInteractableException: element not interactable (Session info: chrome=89.0.4389.114) Stack Trace: RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary 2 parameters) RemoteWebElement.Execute(String commandToExecute, Dictionary 2 parameters) RemoteWebElement.Click() TestClass1.CaseInfoTab() line 151我收到以下消息:消息:OpenQA.Selenium.ElementNotInteractableException:元素不可交互(会话信息:chrome=89.0.4389.114)堆栈跟踪:RemoteWebDriver.UnpackAndThrowOnError(响应错误响应)RemoteWebDriver.Execute(字符串 driverCommandToExecute,字典2 parameters) RemoteWebElement.Execute(String commandToExecute, Dictionary 2 参数)RemoteWebElement.Click() TestClass1.CaseInfoTab() 第 151 行

Then I add 10 seconds of wait:然后我添加了 10 秒的等待时间:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("li[data-item-marker=Yes]")));

I got this message:我收到了这条消息:

Message:信息:

OpenQA.Selenium.WebDriverTimeoutException : Timed out after 10 seconds

Stack Trace: DefaultWait 1.ThrowTimeoutException(String exceptionMessage, Exception lastException) DefaultWait 1.Until[TResult](Func`2 condition) TestClass1.CaseInfoTab() line 150堆栈跟踪: DefaultWait 1.ThrowTimeoutException(String exceptionMessage, Exception lastException) DefaultWait 1.Until[TResult](Func`2 条件) TestClass1.CaseInfoTab() 第 150 行

Please see attachment as well也请看附件

Thank you for your help NG谢谢你的帮助NG

在此处输入图像描述

在此处输入图像描述

Your css selector seems to be wrong.Try now.您的 css 选择器似乎有误。立即尝试。 It should be tagname[attributename='attributeval']它应该是tagname[attributename='attributeval']

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
wait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("li[data-item-marker='Yes']")));

Or Use following xpath.或使用以下 xpath。

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//li[@data-item-marker='Yes' and text()='Yes']")));

Update :更新

Try with Java script executor.尝试使用 Java 脚本执行器。

IWebElement SplitCaseYes = driver.FindElement(By.CssSelector("li[data-item-marker='Yes']"));
IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].click();", SplitCaseYes);

Change改变

IWebElement SplitCase = driver.FindElement(By.XPath(".//*[@id='OpportunityPageV2UsrSplitCase503e4272-cdbd-44d2-98c2-e67a2996c717ComboBoxEdit-el']"));

to

IWebElement SplitCase = driver.FindElement(By.XPath("//div[contains(@id,'OpportunityPageV2UsrSplitCase')]"));

I am not sure if there is div In the beginning.我不确定开头是否有div I do not see it on the screenshot.我在屏幕截图上看不到它。 Verify it.验证它。 The locator that you use is not stable.您使用的定位器不稳定。 Also add explicit waits.还要添加显式等待。

Try 2:尝试2:

For SplitCaseYes also try using this css selector: By.CssSelector('li[data-item-marker="Yes"]') or ul>li[data-item-marker=Yes] or their variations.对于SplitCaseYes也尝试使用此 css 选择器: By.CssSelector('li[data-item-marker="Yes"]')ul>li[data-item-marker=Yes]或其变体。

Also, add wait before clicking your element.此外,在单击您的元素之前添加等待。

WebDriverWait wait = new WebDriverWait(PropertiesCollection.driver, TimeSpan.FromSeconds(30));
WebElement el = wait.until(ExpectedConditions.ElementToBeClickable(By.CssSelector("your selector")));
el.click();

Possible Ajax problem可能的 Ajax 问题

There is a probability that your element is not interactable because of Ajax requests:由于 Ajax 请求,您的元素可能无法交互:

Try this before clicking as well:在点击之前尝试一下:

public void WaitForAjax()
{
    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
    wait.Until(d => (bool)(d as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0"));
}

Update 4. There is a probability that locator above is not unique (did you check CSS locators I proposed?) Try the following xpath :更新 4.上面的定位器有可能不是唯一的(您是否检查了我建议的 CSS 定位器?)尝试以下xpath

//div[contains(@data-item-marker,'Split Case')]/ul/li[@data-item-marker='Yes']

This locator first looks at data-item-marker with Split Case text, then goes level down to ul , and finally to the second li element.此定位器首先查看带有拆分大小写文本的data-item-marker ,然后下降到ul ,最后到第二个li元素。 In this case instead of /li[@data-item-marker='Yes'] it's possible to use just li[2]在这种情况下,而不是/li[@data-item-marker='Yes']可以只使用li[2]

Before executing any code with locators, you should check if it's unique.在使用定位器执行任何代码之前,您应该检查它是否是唯一的。

在此处输入图像描述

Update 5: I found out that C#'s Selenium ExpectedConditions is obsolete: C# Selenium 'ExpectedConditions is obsolete' Try using: SeleniumExtras.WaitHelpers.ExpectedConditions . Update 5: I found out that C#'s Selenium ExpectedConditions is obsolete: C# Selenium 'ExpectedConditions is obsolete' Try using: SeleniumExtras.WaitHelpers.ExpectedConditions . You'll need to import it with Nuget package manager.您需要使用 Nuget package 管理器导入它。 Here is the example:这是示例:

var wait = new WebDriverWait(driver, TimeSpan.FromMilliseconds(10000));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.CssSelector("ul>li[data-item-marker=Yes]")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OpenQA.Selenium.ElementNotInteractableException:元素不可交互 - OpenQA.Selenium.ElementNotInteractableException : element not interactable 修复元素不可交互 Selenium C# - Fix Element not Interactable Selenium C# Selenium C# ElementNotVisibleException:元素不可交互但元素实际上是可见的 - Selenium C# ElementNotVisibleException: element not interactable but the element is actually visible OpenQA.Selenium.ElementNotInteractableException 上 Selenium 与 C# - OpenQA.Selenium.ElementNotInteractableException on Selenium with C# C# selenium webdriver dropdown 错误:元素不可交互 - C# selenium webdriver dropdown Error : element not interactable 添加到购物车按钮不起作用,Selenium C# 元素不可交互 - Add to cart button not working, Selenium C# element not interactable 如何处理 selenium c# 中的元素不可交互异常 - how to handle element not interactable exception in selenium c# 检查元素在 C# Selenium/ChromeDriver 应用程序中是否可交互? - Check if element is interactable in C# Selenium/ChromeDriver app? 在带有 selenium c# 的模态对话框中单击文本框时元素不可交互 - element not interactable when Click textbox in modal dialog with selenium c# C#Selenium-OpenQA.Selenium.ElementNotInteractableException-无法将元素滚动到视图中 - C# Selenium - OpenQA.Selenium.ElementNotInteractableException - Element could not be scrolled into view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM