如何解决如何使用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
<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')
嗯...我可以简单地告诉你简单的逻辑
- 默认情况下,当日历打开时,会选择当前日期
- 您需要检查当前日期是否小于或大于要选择的日期。 Date.compareTo函数将在此处提供帮助
- 然后,您需要导航到相应的月份和年份组合。
- 在正确的月份和年份之后,您还可以选择日期。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。