我尝试单击此日历并使用 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/