当前位置: 首页 > 知识库问答 >
问题:

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

汲昊空
2023-03-14

我是新的Selenium C#NUnit。我跟着一行代码跑了

 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(驱动程序,TimeSpan.FromSeconds(30));等待直至(d)=

 SplitCaseYes.Click();

我得到了以下消息:消息:OpenQA. Selenium。元素不可交互(会话信息:chrome=89.0.4389.114)堆栈跟踪:远程WebDriver。Unpack AndThrowOnError(响应错误响应)远程Web驱动程序。Execute(String driverDirecdToExecute,字典2参数)Remote teWebElement.Execute(字符串命令ToExecute,字典2参数)RemteWebElement.单击()TestClass1。CaseInfoTab()第151行

然后我加上10秒的等待时间:

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

我得到了这个信息:

留言:

OpenQA.Selenium.WebDriverTimeoutException : Timed out after 10 seconds

堆栈跟踪:DefaultWait1。ThrowTimeoutException(字符串exceptionMessage,Exception lastException)DefaultWait1。直到[TResult](Func`2条件)TestClass1。CaseInfoTab()第150行

请参阅附件

谢谢你的帮助

共有2个答案

柯捷
2023-03-14

改变

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

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

我不确定是否有div在开始。我没有看到它的屏幕截图。验证一下。您使用的定位器不稳定。还要添加显式等待。

尝试2:

对于SplitCaseYes还可以尝试使用此css选择器:By。CSS选择器('li[数据项标记=“是”]”)ul

另外,在单击元素之前添加wait。

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

可能的Ajax问题

由于Ajax请求,您的元素可能无法交互:

在单击之前,请尝试以下操作:

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

更新4.上面的定位器有可能不是唯一的(你检查了我提出的CSS定位器吗?)尝试以下xpath

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

这个定位器首先用Split Case文本查看data-item-mark,然后向下到ul,最后到第二个li元素。在这种情况下,可以只使用li[2]来代替/li[@data-item-mark='Yes']

在使用定位器执行任何代码之前,您应该检查它是否唯一。

更新5:我发现C#的SeleniumExpectedConditions已经过时:C#SeleniumExpectedConditions已经过时了,请尝试使用:SeleniumExtras。服务员。预期条件。您需要使用Nuget package manager导入它。以下是一个例子:

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

您的css选择器似乎是错误的。现在试试。它应该是标签名[属性名='属性']

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

或者使用以下xpath。

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

更新:

尝试使用Java脚本执行器。

IWebElement SplitCaseYes = driver.FindElement(By.CssSelector("li[data-item-marker='Yes']"));
IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].click();", SplitCaseYes);
 类似资料:
  • 我有两个输入文本 通过打印元素标题,我确认了第一个和第二个输入元素都是可访问的。到目前为止,我尝试了setAttribute,executescript……但没有一个有效(或者我做了一些错误)(也许,它可能对谷歌chrome自动更新有作用?这是我现在唯一能想到的)

  • 我试图得到某个产品的评论,但它返回一个错误。 我的代码: 它返回:硒。常见的例外。ElementNotInteractitableException:消息:元素不可交互 你能告诉我该怎么办吗?

  • 硒的新手!我正在尝试使用selenium在Craigslist上列出待售房屋。 我在从下拉选择框中选择选项时遇到问题。 我收到以下错误: 元素不可操作异常:消息:元素不可交互:元素当前不可见,可能无法操作 使用“选择”也会产生同样的错误: 该元素存在: 但这并不是棘手的: 元素不可交互异常: 消息: 元素不可交互 我注意到选择元素是隐藏的,选择由选择框上的下一个元素控制。 我可以通过激活元素并使用

  • 我正在使用Selenium记录我在网页上的操作,但是,当我运行测试用例时,出现了一个点击操作抛出错误:元素当前不可见,因此可能无法与之交互。但是,我确信按钮是可见的,下面是它的html: 此外,这是我的Selenium IDE测试脚本: 有人知道为什么吗?提前感谢!

  • 我试图登录到beeradvocate.com抓取(抓取)一些啤酒数据。我试过硒,但失败了。 这是html 我尝试使用名称、值和类,但一切都失败了。我最后一次尝试Xpath,但也失败了。 网站和检查 我的代码: 我已经使按钮工作: 然而,我需要能够执行发送_键来输入id和pw来登录。。。有人知道吗?

  • 我需要选择列表中的最后一项。下面的代码显示了该元素当前不可见的消息。如何解决这个问题? HTML: 列表的屏幕截图。该列表有一个搜索字段,用户可以在其中输入前缀以缩小搜索范围。