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

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

How to send date within an input date control with onkeydown="return false" using Selenium Python

问题

我有这个最小的HTML:

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <input type="date" max="2023-03-09" value="2023-03-09" onkeydown="return false">
  5. </body>
  6. </html>

这只是要求输入日期,但 onkeydown="return false" 阻止了键盘输入。因此,我必须使用(我猜是浏览器生成的)日历,但不知道如何访问它。甚至在控件中的日历图标也很难访问。我不得不采用点击固定偏移量的方法,但也许有更好的方法。

我的最小Python代码是:

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver import ActionChains
  4. import time
  5. driver = webdriver.Firefox()
  6. driver.get("E:\\Web\\TestDate\\public_html\\index.html")
  7. buttonDate = driver.find_element(By.TAG_NAME, "input")
  8. action = ActionChains(driver)
  9. w, h = buttonDate.size['width'], buttonDate.size['height']
  10. x, y = buttonDate.location['x'], buttonDate.location['y']
  11. wx, wy = driver.get_window_size()['width'], driver.get_window_size()['height']
  12. action.move_to_element_with_offset(buttonDate, w - 10, h - 7)
  13. action.click()
  14. action.perform()
  15. time.sleep(30)
  16. driver.quit()

通过这样做,我可以打开日历控件,但无法使用 send_keys() 更改日期。

英文:

I have this minimal html:

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html&gt;
  3. &lt;body&gt;
  4. &lt;input type=&quot;date&quot; max=&quot;2023-03-09&quot; value=&quot;2023-03-09&quot; onkeydown=&quot;return false&quot;&gt;
  5. &lt;/body&gt;
  6. &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:

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver import ActionChains
  4. import time
  5. driver = webdriver.Firefox()
  6. driver.get(&quot;E:\\Web\\TestDate\\public_html\\index.html&quot;)
  7. buttonDate = driver.find_element(By.TAG_NAME, &quot;input&quot;)
  8. action = ActionChains(driver)
  9. w, h = buttonDate.size[&#39;width&#39;], buttonDate.size[&#39;height&#39;]
  10. x, y = buttonDate.location[&#39;x&#39;], buttonDate.location[&#39;y&#39;]
  11. wx, wy = driver.get_window_size()[&#39;width&#39;], driver.get_window_size()[&#39;height&#39;]
  12. action.move_to_element_with_offset(buttonDate, w - 10, h - 7)
  13. action.click()
  14. action.perform()
  15. time.sleep(30)
  16. 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

只删除事件处理程序。

  1. driver.execute_script("document.querySelector('input[type=\"date\"]').onkeydown = () => {}")

Javascript 代码

  1. document.querySelector('input[type="date"]').onkeydown = () => {}

完整代码如下

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver import ActionChains
  4. import time
  5. driver = webdriver.Firefox()
  6. driver.get("E:\\Web\\TestDate\\public_html\\index.html")
  7. buttonDate = driver.find_element(By.TAG_NAME, "input")
  8. # 在这里调用 JavaScript。
  9. driver.execute_script("document.querySelector('input[type=\"date\"]').onkeydown = () => {}")
  10. # 现在在元素上发送按键
  11. buttonDate.send_keys("20230629")
  12. action = ActionChains(driver)
  13. time.sleep(30)
  14. driver.quit()

通过jsfiddle在线测试。

英文:

just remove the event handler

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

Javascript code

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

full code such as

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver import ActionChains
  4. import time
  5. driver = webdriver.Firefox()
  6. driver.get(&quot;E:\\Web\\TestDate\\public_html\\index.html&quot;)
  7. buttonDate = driver.find_element(By.TAG_NAME, &quot;input&quot;)
  8. # Call Javascript Here.
  9. driver.execute_script(&quot;document.querySelector(&#39;input[type=\&quot;date\&quot;]&#39;).onkeydown = () =&gt; {}&quot;)
  10. # Now send the keys here to the element
  11. buttonDate.send_keys(&quot;20230629&quot;)
  12. action = ActionChains(driver)
  13. time.sleep(30)
  14. driver.quit()

Online Test via jsfiddle

答案2

得分: 1

你可以使用Selenium的execute_script方法来运行JavaScript,并将文本传递到所需的文本框中。参考下面的代码:

  1. date= "2023-03-09"
  2. input_box = driver.find_element(By.XPATH, "//input[@type='date']")
  3. 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:

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

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <input type="date" max="2023-03-09" value="2023-03-09" onkeydown="return false">
  5. </body>
  6. </html>

要发送自定义日期,您可以使用removeAttribute()方法来移除onkeydown属性,然后按如下方式调用send_keys()

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

浏览器快照:

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

参考文献:

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

英文:

Given the HTML:

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html&gt;
  3. &lt;body&gt;
  4. &lt;input type=&quot;date&quot; max=&quot;2023-03-09&quot; value=&quot;2023-03-09&quot; onkeydown=&quot;return false&quot;&gt;
  5. &lt;/body&gt;
  6. &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:

  1. driver.get(&quot;file:///C:/Users/fname.lname/Desktop/My%20Documents/Selenium/date.html&quot;)
  2. 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;))))
  3. 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()函数直接更改日期:

  1. script = (
  2. """document.querySelector('%s').setAttribute('%s','%s');"""
  3. % ('input[type="date"]', "value", "2023-06-29")
  4. )
  5. driver.execute_script(script)
英文:

You can use the JS setAttribute() function to change the date directly:

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

确定