admin管理员组文章数量:1278822
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.webdrivermon.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('.nsf/AppSelect?OpenForm')
# this is the problem
driver.find_element_by_id('GNIBExDT').send_keys(10/08/2019)
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.mon.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)
Share
Improve this question
edited Aug 11, 2019 at 21:49
undetected Selenium
193k44 gold badges303 silver badges378 bronze badges
asked Aug 11, 2019 at 10:57
Paul NobregaPaul Nobrega
431 silver badge4 bronze badges
2 Answers
Reset to default 8It'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'")
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.
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.mon.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./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
)
The <input>
field associated with the <label>
Date of Birth is having the attribute readonly. So to invoke send_keys()
you have to:
Scroll to bring the element within the viewport.
Use
execute_script()
to remove thereadonly
attribute.Invoke
send_keys()
to send the date.You can use the following solution:
Code Block:
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.mon.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")
Browser Snapshot:
本文标签:
版权声明:本文标题:javascript - How to send a date directly as text to a calendar control with readonly attribute using Selenium through Python? - 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741270853a2369265.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论