我要选择用户指定的日期,而选择器的代码段在下面。
sendkeys()功能无效,所以我尝试了以下代码。
JavascriptExecutor check = (JavascriptExecutor)driver;
check.executeScript("document.getElementById('hotel-checkin').setAttribute('value','10 Jan 2018')");
在输入值时,日期选择器保持活动状态,其中脚本无法单击实际上与日期选择器重叠的搜索按钮。
任何线索都会有很大的帮助。提前谢谢!
答案 0 :(得分:0)
好的,你要求提供线索,所以这里有一些想法和猜测(只要你没有给我们提供url和html)。
driver.execute_script("arguments[0].value = arguments[1];", webelement, value)
希望,这很有帮助。
编辑。至于第3段。这是我有点编辑的例子(python,再次)。
datepicker看起来像这样 正如您所看到的,它有按钮来调整月份和年份。
from selenium.webdriver.support import expected_conditions as EC
class SetValCalendarStrategy(object):
def __init__(self, driver, calendar_param):
self.driver = driver
# this object holds parameters to find the calendar itself and its components
# pairs:
# sel_type - selector type ('xpath', 'id', 'name' etc.)
# sel_value - selector value to find ("//tr/td" etc)
self.param = calendar_param
def __call__(self, field, value, timeout):
"""
:param <webelement> field - input field
:param <datetime> value - value (date) to set
"""
# initiate datepicker with click on date input field
field.click()
# wait for a widget to show
cal = WebDriverWait(self.driver, timeout, 0.3).until(
EC.visibility_of_element_located(
(self.param.sel_type, self.param.sel_value)))
# decrease month/year button
prev_button = cal.find_element(
self.param.prev_month.sel_type,
self.param.prev_month.sel_value)
# increase button
next_button = cal.find_element(
self.param.next_month.sel_type,
self.param.next_month.sel_value)
today = datetime.now()
# calculate difference in months between today and the target date
month_diff = value.month + (value.year - today.year) * 12 - today.month
# select month/date
if month_diff < 0:
button = prev_button
else:
button = next_button
for i in range(abs(month_diff)):
button.click()
# template looks like this. It selects only days from target month (bold font)
# "//div[contains(@class, 'datePickerDay') and not(contains(@class, 'datePickerDayIsFiller')) and text()='{}']"
# insert day (21) into template. then it becomes
# "//div[contains(@class, 'datePickerDay') and not(contains(@class, 'datePickerDayIsFiller')) and text()='21']"
day_picker_sel_value =
self.param.day_picker.sel_template.format(value.day)
day = cal.find_element(
self.param.day_picker.sel_type,
day_picker_sel_value)
day.click()