无法使用Selenium WebDriver中的sendKeys输入日期

日期字段就像一个日历,我无法使用Selenium WebDriver的sendKeys输入日期。

但是在使用Selenium RC之前,日期字段中的“类型”工作正常。

我尝试在“sendKeys()”之前使用“clear()”,但这给出了错误:

Caught Exception: Element is read-only and so may not be used for actions Command duration or timeout: 10.11 seconds 

sendKeys()适用于其他文本输入字段。

我试过isDisplayed()检查元素,它是真的。 即使在浏览器中,当运行测试时,光标也会转到日期字段,但不会在其中键入任何文本。

使用以下代码…

 ((JavascriptExecutor)driver).executeScript ("document.getElementById('dateofbirth').removeAttribute('readonly',0);"); WebElement BirthDate= driver.findElement(By.id("dateofbirth")); BirthDate.clear(); BirthDate.sendKeys("20-Aug-1985"); //Enter this date details with valid date format 

我也遇到了同样的问题。这就是我找到的解决方案。这对我来说很好。 只需删除输入字段的只读属性,然后像其他输入字段一样。

  ((JavascriptExecutor) driver).executeScript("document.getElementsByName('date'[0].removeAttribute('readonly');"); WebElement dateFld = driver.findElement(By.id("date_completed")); dateFld.clear(); dateFld.sendKeys("date Completed"); 

如果您使用的是jQuery日期选择器对象,则该字段应为只读,并且必须从日历对象中选择日期。 在这种情况下,您可以使用Selenium Web Driver的“选择”类方法来选择日期。

 Select date = new Select(driver.findElement(By.linkText("the date want to select"))); date.click(); 

我做到了这一点并且效果很好。 照顾格式。 通过这个,您甚至可以从控件中获取值。

 var dob = element(by.id('dateOfBirth')) dob.sendKeys('20-08-1985'); expect(element(by.id('dateOfBirth')).getAttribute('value')).toBe('2015-20-08'); 

希望能帮助到你。

对于此线程的未来读者,@ Flalagan发布的问题https://github.com/mozilla/geckodriver/issues/1070的解决方案在Win7-64上与Firefox 63一起使用

“为了记录,它看起来像具有正确格式的ISO日期(yyyy-mm-dd)的send_keys有效。所以对于你的例子,你可以尝试用(如)2012-11-02调用send_keys吗?”