如何通过 Python 使用 Selenium 将日期作为文本直接发送到具有只读属性的日历控件?
How to send a date directly as text to a calendar control with readonly attribute using Selenium through Python?
我正在尝试 select 使用 python 和 selenium 形成日历的日期,但我需要一些帮助,我在 VBA 中做了这个,但我想在python。
提前致谢。
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)
这是一个 read-only 输入 - 如果您查看 HTML src,它有 readonly
属性。这意味着 send_keys
,它通过模拟按键来工作,就好像它是一个真实的用户(也触发任何监听输入变化的事件监听器),是 尝试 来键入你的价值,但不能,因为它是 read-only。但是,您仍然可以手动设置它 - 尝试:
driver.execute_script("document.getElementById('GNIBExDT').value = '10/08/2019'")
这会执行以下JS代码:
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.
他们似乎只是在示例网站上使用基本字符串来表示日期,因为它是一个自定义日期选择器。所以,他们没有做任何特别的事情,例如使用实际的日期格式或日期 objects。但是,由于基于标题,这就是您想要的,我将举一个例子,供其他用 Google 搜索过此问题的人这样做:
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
)
与 <label>
出生日期 关联的 <input>
字段具有 readonly 属性。所以要调用 send_keys()
你必须:
- 滚动以将元素置于视口内。
- 使用
execute_script()
删除 readonly
属性。
- 调用
send_keys()
发送日期。
您可以使用以下解决方案:
代码块:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver.get("https://burghquayregistrationoffice.inis.gov.ie/Website/AMSREG/AMSRegWeb.nsf/AppSelect?OpenForm")
dob = driver.find_element_by_css_selector("input#DOB")
driver.execute_script("window.scrollBy(0, 400)")
driver.execute_script("arguments[0].removeAttribute('readonly')", dob);
driver.find_element_by_css_selector("input#DOB").send_keys("10/08/2019")
浏览器快照:
我正在尝试 select 使用 python 和 selenium 形成日历的日期,但我需要一些帮助,我在 VBA 中做了这个,但我想在python。 提前致谢。
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)
这是一个 read-only 输入 - 如果您查看 HTML src,它有 readonly
属性。这意味着 send_keys
,它通过模拟按键来工作,就好像它是一个真实的用户(也触发任何监听输入变化的事件监听器),是 尝试 来键入你的价值,但不能,因为它是 read-only。但是,您仍然可以手动设置它 - 尝试:
driver.execute_script("document.getElementById('GNIBExDT').value = '10/08/2019'")
这会执行以下JS代码:
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.
他们似乎只是在示例网站上使用基本字符串来表示日期,因为它是一个自定义日期选择器。所以,他们没有做任何特别的事情,例如使用实际的日期格式或日期 objects。但是,由于基于标题,这就是您想要的,我将举一个例子,供其他用 Google 搜索过此问题的人这样做:
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
)
与 <label>
出生日期 关联的 <input>
字段具有 readonly 属性。所以要调用 send_keys()
你必须:
- 滚动以将元素置于视口内。
- 使用
execute_script()
删除readonly
属性。 - 调用
send_keys()
发送日期。 您可以使用以下解决方案:
代码块:
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC driver.get("https://burghquayregistrationoffice.inis.gov.ie/Website/AMSREG/AMSRegWeb.nsf/AppSelect?OpenForm") dob = driver.find_element_by_css_selector("input#DOB") driver.execute_script("window.scrollBy(0, 400)") driver.execute_script("arguments[0].removeAttribute('readonly')", dob); driver.find_element_by_css_selector("input#DOB").send_keys("10/08/2019")
浏览器快照: