英文:
How to send date within an input date control with onkeydown="return false" using Selenium Python
问题
我有这个最小的HTML:
<!DOCTYPE html>
<html>
<body>
<input type="date" max="2023-03-09" value="2023-03-09" onkeydown="return false">
</body>
</html>
这只是要求输入日期,但 onkeydown="return false"
阻止了键盘输入。因此,我必须使用(我猜是浏览器生成的)日历,但不知道如何访问它。甚至在控件中的日历图标也很难访问。我不得不采用点击固定偏移量的方法,但也许有更好的方法。
我的最小Python代码是:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import time
driver = webdriver.Firefox()
driver.get("E:\\Web\\TestDate\\public_html\\index.html")
buttonDate = driver.find_element(By.TAG_NAME, "input")
action = ActionChains(driver)
w, h = buttonDate.size['width'], buttonDate.size['height']
x, y = buttonDate.location['x'], buttonDate.location['y']
wx, wy = driver.get_window_size()['width'], driver.get_window_size()['height']
action.move_to_element_with_offset(buttonDate, w - 10, h - 7)
action.click()
action.perform()
time.sleep(30)
driver.quit()
通过这样做,我可以打开日历控件,但无法使用 send_keys()
更改日期。
英文:
I have this minimal html:
<!DOCTYPE html>
<html>
<body>
<input type="date" max="2023-03-09" value="2023-03-09" onkeydown="return false">
</body>
</html>
That just asks for a date, but onkeydown="return false"
prevents keyboard input. So I have to navigate the (I guess browser-generated) calendar, but don't know how to access it. Even the calendar icon in the control is difficult to access. I have resorted to clicking with a fixed offset, but maybe there's a better way.
My minimal python code is:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import time
driver = webdriver.Firefox()
driver.get("E:\\Web\\TestDate\\public_html\\index.html")
buttonDate = driver.find_element(By.TAG_NAME, "input")
action = ActionChains(driver)
w, h = buttonDate.size['width'], buttonDate.size['height']
x, y = buttonDate.location['x'], buttonDate.location['y']
wx, wy = driver.get_window_size()['width'], driver.get_window_size()['height']
action.move_to_element_with_offset(buttonDate, w - 10, h - 7)
action.click()
action.perform()
time.sleep(30)
driver.quit()
With that I can get the calendar control open, but cannot use send_keys()
to change the date.
Edit: Thanks for all the answers, you all saved me. I have accepted the shortest, most general purpose one, even if all were good.
答案1
得分: 1
只删除事件处理程序。
driver.execute_script("document.querySelector('input[type=\"date\"]').onkeydown = () => {}")
Javascript 代码
document.querySelector('input[type="date"]').onkeydown = () => {}
完整代码如下
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import time
driver = webdriver.Firefox()
driver.get("E:\\Web\\TestDate\\public_html\\index.html")
buttonDate = driver.find_element(By.TAG_NAME, "input")
# 在这里调用 JavaScript。
driver.execute_script("document.querySelector('input[type=\"date\"]').onkeydown = () => {}")
# 现在在元素上发送按键
buttonDate.send_keys("20230629")
action = ActionChains(driver)
time.sleep(30)
driver.quit()
通过jsfiddle在线测试。
英文:
just remove the event handler
driver.execute_script("document.querySelector('input[type=\"date\"]').onkeydown = () => {}")
Javascript code
document.querySelector('input[type="date"]').onkeydown = () => {}
full code such as
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import time
driver = webdriver.Firefox()
driver.get("E:\\Web\\TestDate\\public_html\\index.html")
buttonDate = driver.find_element(By.TAG_NAME, "input")
# Call Javascript Here.
driver.execute_script("document.querySelector('input[type=\"date\"]').onkeydown = () => {}")
# Now send the keys here to the element
buttonDate.send_keys("20230629")
action = ActionChains(driver)
time.sleep(30)
driver.quit()
Online Test via jsfiddle
答案2
得分: 1
你可以使用Selenium的execute_script
方法来运行JavaScript
,并将文本传递到所需的文本框中。参考下面的代码:
date= "2023-03-09"
input_box = driver.find_element(By.XPATH, "//input[@type='date']")
driver.execute_script('arguments[0].value=arguments[1]', input_box, date)
注意: 这就像从后端向网页插入文本一样。这种解决方案不会模拟人类的动作,就像Selenium的send_keys
做的那样。
英文:
You can use Selenium's execute_script
method to run JavaScript
and pass the text into the desired textbox. Refer the code below:
date= "2023-03-09"
input_box = driver.find_element(By.XPATH, "//input[@type='date']")
driver.execute_script('arguments[0].value=arguments[1]', input_box, date)
Note: This is like inserting the text into the web page from backend. This solution won't imitate the human actions as selenium's send_keys
does.
答案3
得分: 1
给定的HTML:
<!DOCTYPE html>
<html>
<body>
<input type="date" max="2023-03-09" value="2023-03-09" onkeydown="return false">
</body>
</html>
要发送自定义日期,您可以使用removeAttribute()
方法来移除onkeydown属性,然后按如下方式调用send_keys()
:
driver.get("file:///C:/Users/fname.lname/Desktop/My%20Documents/Selenium/date.html")
driver.execute_script("arguments[0].removeAttribute('onkeydown')", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[type='date']"))))
driver.find_element(By.CSS_SELECTOR, "input[type='date']").send_keys("29062023")
浏览器快照:
参考文献:
您可以在以下几个相关的详细讨论中找到更多信息:
英文:
Given the HTML:
<!DOCTYPE html>
<html>
<body>
<input type="date" max="2023-03-09" value="2023-03-09" onkeydown="return false">
</body>
</html>
To send a customized date you can use the removeAttribute()
method to remove the onkeydown attribute and invoke send_keys()
as follows:
driver.get("file:///C:/Users/fname.lname/Desktop/My%20Documents/Selenium/date.html")
driver.execute_script("arguments[0].removeAttribute('onkeydown')", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[type='date']"))))
driver.find_element(By.CSS_SELECTOR, "input[type='date']").send_keys("29062023")
Browser snapshot:
References
You can find a couple of relevant detailed discussions in:
答案4
得分: 1
你可以使用JS的setAttribute()
函数直接更改日期:
script = (
"""document.querySelector('%s').setAttribute('%s','%s');"""
% ('input[type="date"]', "value", "2023-06-29")
)
driver.execute_script(script)
英文:
You can use the JS setAttribute()
function to change the date directly:
script = (
"""document.querySelector('%s').setAttribute('%s','%s');"""
% ('input[type="date"]', "value", "2023-06-29")
)
driver.execute_script(script)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论