如何使用selenium处理具有type =“ date”的html日期选择器,日期选择在html dom中没有变化

问题描述

我正在尝试处理基于html的日期选择器,我可以单击它,以便弹出日期选择器日历,但是日历没有检查选项。

即使我手动选择日期,HTML DOM中也没有此类更改,可以使用。 我尝试使用以下解决方法在其中添加日期,但没有一个起作用(不,发生在DOM中的事情)。

jse.executeScript("document.getElementById('date-input').value='10101990'"); 
driver.switchTo().activeElement().sendKeys(Keys.TAB);
driver.findElement(By.id("date-input")).sendKeys(Keys.TAB);

在网络中,它与从日历选项中选择的选项一起显示为可编辑的(运行下面的代码段) 而在移动设备(网络)中,其外观与屏幕截图所示不同。

JAVA-Selenium是否有任何解决方案:Inspect HTML5 date picker shadow DOM

enter image description here

<label for="start">Start date:</label>
<input type="date" id="start" name="trip-start"
       min="2020-01-01" max="2020-12-31" value>
<input class="Input-module_input__nj1kl" type="date" id="date-input" data-testid="date-input" placeholder="Date of Birth" required="" aria-invalid="false" aria-describedby="birth-date-input-error" aria-required="true" min="1920-01-01" max="2020-08-18" value>
<label for="date-input" class="Input-module_label__npqrt1">Date of Birth*</label><label for="date-input" class="Input-module_label__30ytr">Date of Birth*</label>
<span role="alert" id="date-input-error" class="Input-module_errorMessage__lMKY" style="display: none;">Please enter a valid date of birth</span>

解决方法

以下代码示例对我有用。用yyyy-MM-dd格式为变量date

更新所需的日期
    driver.get("http://127.0.0.1:8887/");
    JavascriptExecutor js = (JavascriptExecutor) driver;  
    String date="2009-02-09";
    js.executeScript("document.getElementById('date-input').value ='"+date.toString()+"'",2000);

在js执行器中的Id中更改所需的getElementById('date-input')

,

嗯...我可以简单地告诉你简单的逻辑

  1. 默认情况下,当日历打开时,会选择当前日期
  2. 您需要检查当前日期是否小于或大于要选择的日期。 Date.compareTo函数将在此处提供帮助
  3. 然后,您需要导航到相应的月份和年份组合。
  4. 在正确的月份和年份之后,您还可以选择日期。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...