angular - 元素状态无效 : Element must be user-editable in order to clear it error trying to click and insert a date on a dropdown-toggle using Selenium

标签 angular selenium selenium-webdriver webdriverwait expected-condition

我尝试单击此日历并使用 selenium 自动插入日期,但出现以下错误:

invalid element state: Element must be user-editable in order to clear it.

HTML 片段

<a id="enddate-dropdown" class="dropdown-toggle" role="button" data-toggle="dropdown" data-target="">
                <p class="custom-datepickers__date-prefix ng-binding">To:</p>
                <!-- ngIf: displayEndDate -->
                <!-- ngIf: !displayEndDate --><div ng-if="!displayEndDate" class="custom-datepickers__no-date ng-scope"></div><!-- end ngIf: !displayEndDate -->
</a>

代码片段

myclass.SetDateByXpath("//*[@id=\"enddate-dropdown\"]/p", myclass.GetDate("yyyy/MM/dd", mydate));

public void SetDateByXpath(String element, String value)
    {
        WebElement webElement = ExplicitWaitOnElement(By.xpath(element));       
        ((JavascriptExecutor) driver).executeScript(
                "arguments[0].removeAttribute('readonly','readonly')",webElement);
        webElement.clear();
        webElement.sendKeys(value);
    }

如果我手动设置日期,这是 HTML:

<a id="enddate-dropdown" class="dropdown-toggle" role="button" data-toggle="dropdown" data-target="">
                <p class="custom-datepickers__date-prefix ng-binding">To:</p>
                <!-- ngIf: displayEndDate --><p ng-if="displayEndDate" class="ng-binding ng-scope">2019/11/21</p><!-- end ngIf: displayEndDate -->
                <!-- ngIf: !displayEndDate -->
</a>

可能是网站变了,但现在我不知道如何设置这个值。任何帮助将不胜感激。

谢谢

最佳答案

我猜你的错误是在以下几行抛出的:

webElement.clear();
webElement.sendKeys(value)

我们可以尝试通过 Javascript 设置值来解决这个问题:

// updated selector for webElement to grab the p element which contains the date string
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"enddate-dropdown\"]/p[@ng-if='displayEndDate']"));

// try setting inner HTML which is the text inside the p
((JavascriptExecutor) driver).executeScript("arguments[0].innerHtml = '" + value + "';", webElement)

// alternative option is to try setting value instead
// ((JavascriptExecutor) driver).executeScript("arguments[0].value = '" + value + "';", webElement)

关于angular - 元素状态无效 : Element must be user-editable in order to clear it error trying to click and insert a date on a dropdown-toggle using Selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58881440/

相关文章:

python - 如何使用 python 和 selenium 在新选项卡中打开链接

google-chrome - 如何在 Selenium (C#) 中自动打开 Chrome Devtools 选项卡?

python - Selenium/Python - 如何按随机选择的像素滚动?

javascript - Angular 4 Routes在 Angular 启动器中加号和尖锐符号的含义?

google-chrome - 适用于 ChromeDriver 和 Chrome 浏览器的 Selenium 以及日志消息 "Only local connections are allowed"

javascript - 以 Angular 2 方式获取 DOM 元素的属性

java - 如何使用 java 在 selenium 中显式等待 driver.get()

selenium - NoSuchElementException,Selenium 无法定位元素

Angular 2 仪表控制

angular - 如何在网络应用程序中集成谷歌文档(预览、编辑、保存)