【问题标题】:InvalidElementStateException when attempting to clear text through Selenium尝试通过 Selenium 清除文本时出现 InvalidElementStateException
【发布时间】:2018-05-25 11:40:05
【问题描述】:

我正在测试的 Web 应用程序在删除记录时需要确认。

我创建了一个测试来输入删除此记录的正当理由。

执行此操作的代码如下:

DeleteFieldButton.
            Click();
        WaitMethods.WaitForElementToBeDisplayed(RemovalReason, 20);
        RemovalReason
            .Click();
        RemovalReason
            .Clear();
        RemovalReason
            .SendKeys("Automated Test - Delete Field");

文本框的 XPath 如下:

private Label RemovalReason = new Label(By.XPath("//*[@placeholder = 'Removal reason']"));

每当运行测试时,都会返回以下异常。

OpenQA.Selenium.InvalidElementStateException
HResult=0x80131500
Message=Cannot clear By.XPath: //*[@placeholder = 'Removal reason'] as it is 
not declared as a writable text element
Source=web
StackTrace:
at web.Elements.Common.Element.Clear()
at Daera.Efs.Test.E2e.PageObjects.ApplicationSummary.DeleteField(String 
FieldIDToDelete) in 
C:\Code\Local\WebApp.E2e\PageObjects\ApplicationSummary.cs:line 280
 at Test.E2e.Tests.ApplicationSummaryTest.Delete_Wider_General_Field_From_Application_Summary() in C:\Code\Local\WebApp.E2e\Tests\ApplicationSummaryTest.cs:line 45

以下是 Web 应用程序中元素的 HTML:

<input ng-keypress="dialog.keypress($event)" md-autofocus="" ng-model="dialog.result" placeholder="Removal reason" class="ng-valid md-input ng-not-empty md-autofocus ng-touched ng-dirty ng-valid-parse" aria-label="Removal reason" id="input_22" aria-invalid="false" style="">

【问题讨论】:

  • 用相关的 HTML 更新问题,并请阅读screenshot of HTML or code or error is a bad idea 的原因。考虑使用基于格式化文本的相关 HTML、代码试验和错误堆栈跟踪来更新问题。
  • RemovalReason 元素是按钮还是文本框。根据错误,RemovalReason 元素是按钮,因此在清除操作期间会抛出错误。可以分享一下相关的 HTML 吗?

标签: c# selenium selenium-webdriver xpath webdriverwait


【解决方案1】:

根据 HTML,您已将 &lt;input&gt; 元素与 placeholder attribute 共享为 Removal reason 是一个 Angular 元素并且作为您必须发送文本,您必须诱导 WebDriverWait 以使 元素可点击,如下所示:

IWebElement myElem = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@class='ng-valid md-input ng-not-empty md-autofocus ng-touched ng-dirty ng-valid-parse' and starts-with(@id,'input_') and @placeholder='Removal reason']")));
myElem.Click();
myElem.Clear();
myElem.SendKeys("Automated Test - Delete Field");

参考文献

您可以在以下位置找到一些相关讨论:

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,只是我试图在不允许破折号的日期字段中输入破折号。我通过手动输入带有破折号的日期来解决这个问题,但它没有填充。 (该字段也不接受字母 - 仅接受数字)。现在,我的测试输入了一个没有破折号的日期,它成功通过了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 2023-02-03
      • 1970-01-01
      • 2022-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多