尝试通过Selenium清除文本时发生InvalidElementStateException [英] InvalidElementStateException when attempting to clear text through Selenium

查看:591
本文介绍了尝试通过Selenium清除文本时发生InvalidElementStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The web application I am testing requires confirmation when deleting a record.

我创建了一个测试,以输入删除该记录的正当理由.

I have created a test to enter in a valid reason for deleting this record.

执行此操作的代码如下:

The code to do this is as follows:

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

文本框的XPath如下:

The XPath to the text box is as follows:

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

无论何时运行测试,都会返回以下异常.

Whenever the test is run the following exception is returned.

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:

Below is the HTML of the element on the web application:

<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,您已将<input>元素与placeholder 属性共享为删除原因 >是 Angular 元素,并且您必须发送文本,因此必须诱使 WebDriverWait 使元素可点击,如下所示:

As per the HTML you have shared the <input> element with placeholder attribute as Removal reason is an Angular element and as you have to send text, you have to induce WebDriverWait for the element to be clickable as follows :

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");


参考文献

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


References

You can find a couple of relevant discussions in:

  • How to clear text field before sending keys selenium c#
  • clear() does not clear the textbox with selenium and python and firefox
  • python selenium can't clear input field

这篇关于尝试通过Selenium清除文本时发生InvalidElementStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆