[英]Trouble sending JavaScript into <input type="date"> calendar
Goal: Attempting to automate a calendar on Chrome using Selenium(Python + behave).目标:尝试使用 Selenium(Python + 行为)在 Chrome 上自动化日历。
The issue: The date is only being set when actually clicked through the calendar.问题:仅在实际单击日历时才设置日期。 Passing a value via JavaScript is not working.通过 JavaScript 传递值不起作用。
Note: According to docs the inputs should allow users to send input in with keys by default but that doesn't even work.注意:根据文档,默认情况下输入应该允许用户使用键发送输入,但这甚至不起作用。 It does not accept keyboard input at all.它根本不接受键盘输入。
Problem Walkthrough问题演练
x = document.querySelector("#class > something").shadowRoot.querySelector("div > div > input[type=date]:nth-child(5)") #Select input for From Date
x = "2022-05-12"
y = document.querySelector("#class > something").shadowRoot.querySelector("div > div > input[type=date]:nth-child(5)") #Select input for To Date
y = "2022-06-12"
The HTML for the calendar is:日历的 HTML 是:
<input type="date" min="2022-01-01" max="2022-07-19">
Clearly.清楚地。 The JavaScript alone does not seem to be enough.仅 JavaScript 似乎还不够。 If you use the same method above on this calendar it will work.如果您在此日历上使用上述相同的方法,它将起作用。 However, on the calendar I am attempting this JS method will not work for some reason.但是,在日历上,我正在尝试这种 JS 方法由于某种原因不起作用。 Also, I cannot get Selenium to click and open the calendar on its own for some odd reason.此外,出于某种奇怪的原因,我无法让 Selenium 自行单击并打开日历。
Solution: Manually trigger the event ( Source )解决方案:手动触发事件( Source )
let element = document.getElementById(id);
element.dispatchEvent(new Event("change")); // or whatever the event type might be
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.