如何通过Python使用Selenium将日期直接以文本形式直接发送到具有readonly属性的日历控件? [英] How to send a date directly as text to a calendar control with readonly attribute using Selenium through Python?

查看:87
本文介绍了如何通过Python使用Selenium将日期直接以文本形式直接发送到具有readonly属性的日历控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python和硒从日历中选择日期,但是我需要一些帮助,我在VBA中做到了,但我想在python中做到这一点.预先感谢.

I'm trying to select a date form a calendar with python and selenium but I need some help, I did this in VBA but I want to do this in python. Thanks in advance.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver=webdriver.Firefox(executable_path=r'..\geckodriver.exe')
driver.get('https://burghquayregistrationoffice.inis.gov.ie/Website/AMSREG/AMSRegWeb.nsf/AppSelect?OpenForm')    

# this is the problem
driver.find_element_by_id('GNIBExDT').send_keys(10/08/2019)

推荐答案

这是只读输入-如果您查看HTML src,则具有 readonly 属性.这意味着 send_keys 可以尝试来键入,它可以模拟按键操作,就好像它是真实用户一样(也可以触发任何事件侦听器来监听输入中的更改).您的值,但不能,因为它是只读的.但是,您仍然可以手动设置它-试试:

It's a read-only input - if you look in the HTML src, it's got the readonly attribute. This means that send_keys, which works by emulating key presses as if it were a real user (to also trigger any eventlisteners listening for change in the input), is trying to type your value, but can't, since it's read-only. However, you can still set it manually - try:

driver.execute_script("document.getElementById('GNIBExDT').value = '10/08/2019'")

这将执行以下JS代码:

This executes the following JS code:

document.getElementById('GNIBExDT') // Equivalent of driver.find_element_by_id('GNIBExDT') in pure JS
    .value = // Used to set the 'value' of the input, which is what will be read on the backend when the form is submitted. This just sets the value directly, so it doesn't matter if it's read-only.
    '10/08/2019' // The date, in string form.

由于它们是自定义的日期选择器,因此它们似乎只是在示例网站上使用基本字符串来表示日期.因此,他们没有做任何特别的事情,例如使用实际的日期格式或Date对象.但是,由于基于标题,这就是您想要的,因此,我将举一个例子来说明对此问题进行过Google搜索的其他人:

It seems like they're just using basic strings on the example website to represent dates, since it's a custom datepicker. So, they're not doing anything special, such as using actual date formats or Date objects. However, since based on the title this is what you'd like, I'll give an example to do such for anyone else who's Googled this problem:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver=webdriver.Firefox(executable_path=r'..\geckodriver.exe')
driver.get('https://www.w3schools.com/html/tryit.asp?filename=tryhtml_input_date')

driver.execute_script("document.getElementsByTagName('input')[0]"  # Get the date picker from the DOM
                     +".valueAsDate"  # Set the value *as a date object* - this is only available in real date pickers (`<input type='date'>`)
                     +" = new Date('2020-03-11')"  # We therefore need to define it as a date object, which we do in 'yyyy-mm-dd hh:mm:ss GMT+hhmm' format
)

这篇关于如何通过Python使用Selenium将日期直接以文本形式直接发送到具有readonly属性的日历控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆