在日历日期选择器上使用 Selenium [英] Using Selenium on calendar date picker

查看:37
本文介绍了在日历日期选择器上使用 Selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从此页面上的日历中选择日期 (01/01/2011).https://cotthosting.com/NYRocklandExternal/LandRecords/protected/SrchQuickName.aspx

I am trying to pick a date (01/01/2011) from a calendar on this page. https://cotthosting.com/NYRocklandExternal/LandRecords/protected/SrchQuickName.aspx

日历位于显示 Date: FROM 的表单部分.当我单击它时,会弹出一个日历供您选择日期.但是,该字段还允许您输入日期.鉴于日历的复杂性,我选择使用 send_keys() 但它不起作用.

The calendar is on the part of the form that says Date: FROM. When I click it, a calendar pops up for you to pick dates. However, the field also allows you type in a date. Given the complexity of calendars, I have chosen to use send_keys() but it is not working.

我已通过其 ID 识别空字段日期字段,但由于某种原因它没有填写表单.当我尝试时:

I have identified the empty field date field by its ID but for some reason it does not fill the form. when I try:

driver.find_element_by_id('ctl00_cphMain_SrchDates1_txtFiledFrom').send_keys("01012011")

关于如何以不同方式操纵它的任何想法?我正在使用带有 Selenium 和 ChromeDriver 的 Python 2.7

Any ideas on how I can maneuver it differently? I'm using Python 2.7 with Selenium and ChromeDriver

推荐答案

要使其正常工作,请添加一个额外的步骤,即在发送键之前单击元素:

To get this to work, add one extra step of clicking the element before sending the keys:

datefield = driver.find_element_by_id('ctl00_cphMain_SrchDates1_txtFiledFrom')
datefield.click()
datefield.send_keys("01012011")

更新:

看起来您可能必须使用 ActionChains 毕竟在您的情况下,这将允许您将一系列操作链接在一起,然后一个接一个地执行它们:

Update:

It looks like you might have to use ActionChains after all in your case, which will allow you to chain a series of actions together, and then perform them one after the other:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()

driver.get("https://cotthosting.com/NYRocklandExternal/User/Login.aspx")
driver.find_element_by_id('ctl00_cphMain_blkLogin_btnGuestLogin').click()

driver.find_element_by_id('ctl00_cphMain_SrchNames1_txtFirmSurName').send_keys("Adam")

datefield = driver.find_element_by_id('ctl00_cphMain_SrchDates1_txtFiledFrom')

ActionChains(driver).move_to_element(datefield).click().send_keys('01012011').perform()
search_btn = driver.find_element_by_id('ctl00_cphMain_btnSearchAll')
ActionChains(driver).move_to_element(search_btn).click().click().perform()

我不确定为什么在这种情况下需要两次 click() 调用,但似乎它们是必要的.我尝试了其他一些方法,包括 double_click(),但这是唯一对我有用的方法,可以使日期字段不聚焦,然后单击搜索按钮.

I am not sure why two click() calls were necessary in this case, but it seems that they were. I tried a few other things including double_click(), but this was the only thing that worked for me to get the datefield unfocused and then click the search button.

这篇关于在日历日期选择器上使用 Selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆