You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Selenium WebDriver(Java)中选择只读Bootstrap日历日期

Got it, let's tackle this Bootstrap readonly calendar issue with Selenium. I’ve run into exactly this kind of dynamic element headache before, so here are a few tried-and-true fixes that should work:

1. Skip the calendar popup entirely—modify the input directly with JavaScript

Most Bootstrap date pickers tie back to an underlying <input> element, even if it’s marked as readonly or hidden. Instead of fighting the disappearing popup, you can target this input directly:

# First, locate the date input element (adjust selector to match your page)
date_input = driver.find_element(By.ID, "your-date-input-id")

# If the input has a readonly attribute, remove it first
driver.execute_script("arguments[0].removeAttribute('readonly');", date_input)

# Now send your desired date (use the format your calendar expects, e.g. YYYY-MM-DD)
date_input.send_keys("2024-05-20")

If the input is hidden (not just readonly), you can set its value directly via JS without needing to interact with it visually:

driver.execute_script("document.getElementById('your-date-input-id').value = '2024-05-20';")

2. Catch the calendar element before it disappears with explicit waits

Sometimes the popup vanishes because you’re not waiting long enough for it to render. Use Selenium’s explicit waits to pause execution until the calendar elements are available:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Click the trigger to open the calendar popup
calendar_trigger = driver.find_element(By.CSS_SELECTOR, ".calendar-trigger-class")
calendar_trigger.click()

# Wait up to 10 seconds for the target date cell to appear, then click it
target_date = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.XPATH, "//td[text()='20']"))
)
target_date.click()

Pro tip: Use visibility_of_element_located instead of presence_of_element_located here—it ensures the element is not just in the DOM, but actually visible and clickable.

3. Freeze the DOM to inspect the calendar (debugging hack)

If you can’t even see the calendar’s HTML in dev tools because it disappears too fast, use this trick:

  1. Open your browser’s dev tools (F12)
  2. Go to the "Sources" tab
  3. Click the calendar trigger to open the popup, then immediately press F8 (this pauses all JavaScript execution, freezing the page)
  4. Now you can inspect the calendar’s DOM structure to find the correct selectors for your Selenium code

4. Use keyboard navigation if the calendar supports it

Some Bootstrap date pickers let you navigate with arrow keys once the popup is open. If that’s the case, you can simulate keyboard inputs instead of clicking elements:

from selenium.webdriver.common.keys import Keys

# Open the calendar popup
calendar_trigger = driver.find_element(By.CSS_SELECTOR, ".calendar-trigger-class")
calendar_trigger.click()

# Navigate to the target date (e.g. press right arrow 5 times, then Enter to select)
calendar_trigger.send_keys(Keys.RIGHT * 5, Keys.ENTER)

内容的提问来源于stack exchange,提问作者Vijayalaxmi Kummari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:16:06