gpt4 book ai didi

java - Selenium 中的清除日期字段

转载 作者:行者123 更新时间:2023-12-02 12:12:14 36 4
gpt4 key购买 nike

我一直在尝试清除 DateField 的内容,但没有成功。当我使用这个时:

driver.findElement(By.id(componentID)).clear();

我收到以下错误:

    org.openqa.selenium.InvalidElementStateException: Element must be user-editable in order to clear it.
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'CMTCLX62137', ip: '53.19.229.176', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_31'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\ALEX\AppData\Local\Temp\rust_mozprofile.eDpIfGCA0yf9, rotatable=false, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, pageLoadStrategy=normal, platform=ANY, proxy=Proxy(manual, http=localhost), specificationLevel=0.0, moz:accessibilityChecks=false, acceptInsecureCerts=true, browserVersion=55.0.3, platformVersion=6.1, moz:processID=9764.0, browserName=firefox, javascriptEnabled=true, platformName=windows_nt}]
Session ID: e1be04d2-d90e-4319-8b55-fc8d147ae48c
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:150)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:115)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:45)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:272)
at org.openqa.selenium.remote.RemoteWebElement.clear(RemoteWebElement.java:114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

我尝试选择所有内容并发送 Keys.DELETE,如下所示:

WebElement element = driver.findElement(By.id(componentID));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.chord("A")).keyUp(Keys.CONTROL).perform();

问题在于它选择了整个页面,而不是 DateField 内的文本。

有人知道如何仅选择字段内的文本,或者以某种方式清除它吗?

[编辑]

这是我要清除的组件的 html:

                <div class="row">
<div class="col-md-4">

<div class="form-group"><label for="Form:asof">As Of</label><span id="Form:asof" class="ui-calendar"><input id="Form:asof_input" name="Form:asof_input" type="text" value="2017-09-26 00:00:00.000" class="ui-inputfield ui-widget ui-state-default ui-corner-all" /></span><script id="Form:asof_s" type="text/javascript">
</div>
</div>
</div>

最佳答案

正如评论中提到的,我给出了以下答案。

由于您正在谈论文本字段,因此它必须是输入标记并尝试下面的行,

driver.findElement(By.id("Form:asof_input")).clear();

希望这有帮助。谢谢。

关于java - Selenium 中的清除日期字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46451069/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com