簡體   English   中英

如何通過 Python 使用 Selenium 將日期作為文本直接發送到具有只讀屬性的日歷控件?

[英]How to send a date directly as text to a calendar control with readonly attribute using Selenium through Python?

我正在嘗試使用 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)

這是一個只讀輸入——如果你查看 HTML src,它有readonly屬性。 這意味着send_keys ,它通過模擬按鍵來工作,就好像它是一個真實的用戶(也觸發任何監聽輸入變化的事件監聽器),正在嘗試輸入您的值,但不能,因為它是只讀的。 但是,您仍然可以手動設置 - 嘗試:

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.

似乎他們只是在示例網站上使用基本字符串來表示日期,因為它是一個自定義日期選擇器。 因此,他們沒有做任何特別的事情,例如使用實際的日期格式或 Date 對象。 但是,由於根據標題這是您想要的,我將舉一個例子,讓其他任何在谷歌上搜索過這個問題的人都這樣做:

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> Date of Birth關聯的<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")
  • 瀏覽器快照:

出生日期

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM