如何使用Selenium Python在输入日期控件中发送日期,使用onkeydown=”return false”。

huangapple go评论78阅读模式
英文:

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:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;body&gt;
        &lt;input type=&quot;date&quot; max=&quot;2023-03-09&quot; value=&quot;2023-03-09&quot; onkeydown=&quot;return false&quot;&gt;
    &lt;/body&gt;
&lt;/html&gt;

That just asks for a date, but onkeydown=&quot;return false&quot; 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(&quot;E:\\Web\\TestDate\\public_html\\index.html&quot;)
buttonDate = driver.find_element(By.TAG_NAME, &quot;input&quot;)
action = ActionChains(driver)
w, h = buttonDate.size[&#39;width&#39;], buttonDate.size[&#39;height&#39;]
x, y = buttonDate.location[&#39;x&#39;], buttonDate.location[&#39;y&#39;]
wx, wy = driver.get_window_size()[&#39;width&#39;], driver.get_window_size()[&#39;height&#39;]
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(&quot;document.querySelector(&#39;input[type=\&quot;date\&quot;]&#39;).onkeydown = () =&gt; {}&quot;)

Javascript code

document.querySelector(&#39;input[type=&quot;date&quot;]&#39;).onkeydown = () =&gt; {}

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(&quot;E:\\Web\\TestDate\\public_html\\index.html&quot;)
buttonDate = driver.find_element(By.TAG_NAME, &quot;input&quot;)

# Call Javascript Here.
driver.execute_script(&quot;document.querySelector(&#39;input[type=\&quot;date\&quot;]&#39;).onkeydown = () =&gt; {}&quot;)

# Now send the keys here to the element
buttonDate.send_keys(&quot;20230629&quot;)

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= &quot;2023-03-09&quot;
input_box = driver.find_element(By.XPATH, &quot;//input[@type=&#39;date&#39;]&quot;)
driver.execute_script(&#39;arguments[0].value=arguments[1]&#39;, 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")

浏览器快照:

如何使用Selenium Python在输入日期控件中发送日期,使用onkeydown=”return false”。

参考文献:

您可以在以下几个相关的详细讨论中找到更多信息:

英文:

Given the HTML:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;body&gt;
        &lt;input type=&quot;date&quot; max=&quot;2023-03-09&quot; value=&quot;2023-03-09&quot; onkeydown=&quot;return false&quot;&gt;
    &lt;/body&gt;
&lt;/html&gt;

To send a customized date you can use the removeAttribute() method to remove the onkeydown attribute and invoke send_keys() as follows:

driver.get(&quot;file:///C:/Users/fname.lname/Desktop/My%20Documents/Selenium/date.html&quot;)
driver.execute_script(&quot;arguments[0].removeAttribute(&#39;onkeydown&#39;)&quot;, WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, &quot;input[type=&#39;date&#39;]&quot;))))
driver.find_element(By.CSS_SELECTOR, &quot;input[type=&#39;date&#39;]&quot;).send_keys(&quot;29062023&quot;)

Browser snapshot:

如何使用Selenium Python在输入日期控件中发送日期,使用onkeydown=”return false”。


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 = (
    &quot;&quot;&quot;document.querySelector(&#39;%s&#39;).setAttribute(&#39;%s&#39;,&#39;%s&#39;);&quot;&quot;&quot;
    % (&#39;input[type=&quot;date&quot;]&#39;, &quot;value&quot;, &quot;2023-06-29&quot;)
)
driver.execute_script(script)

huangapple
  • 本文由 发表于 2023年6月29日 20:22:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定