我有一个最小的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()
来更改日期。
4条答案
按热度按时间332nm8kg1#
只要删除事件处理程序
JavaScript代码
完整代码,如
通过jsfiddle在线测试
rpppsulh2#
您可以使用Selenium的
execute_script
方法来运行JavaScript
并将文本传递到所需的文本框中。参考下面的代码:**注意:**类似于从后台将文本插入网页。这种解决方案不会像selenium的
send_keys
那样模仿人类的行为。hfwmuf9z3#
给定HTML:
要发送自定义日期,可以使用
removeAttribute()
方法删除onkeydown属性,然后调用send_keys()
,如下所示:浏览器快照:
参考资料
您可以在以下内容中找到一些相关的详细讨论:
e0uiprwp4#
可以使用JS
setAttribute()
函数直接修改日期: