在日历日期选择器上使用selenium

时间:2016-09-15 21:33:57

标签: python selenium datepicker

我想从此页面上的日历中选择一个日期(01/01/2011)。 https://cotthosting.com/NYRocklandExternal/LandRecords/protected/SrchQuickName.aspx

日历位于表单Date: FROM的部分。当我点击它时,会弹出一个日历供您选择日期。但是,该字段还允许您键入日期。鉴于日历的复杂性,我选择使用send_keys(),但它无效。

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

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

关于如何以不同的方式操纵它的任何想法?我将Python 2.7与Selenium和ChromeDriver一起使用

3 个答案:

答案 0 :(得分:4)

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

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

更新

看起来你可能不得不在你的情况下使用ActionChains,这将允许你将一系列动作链接在一起,然后一个接一个地执行它们:

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(),但这是唯一能让我没有聚焦的日期,然后点击搜索按钮。

答案 1 :(得分:2)

带有一些解释的替代解决方案:

解决方案

类似于建议的here,执行:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver.get("https://cotthosting.com/NYRocklandExternal/LandRecords/protected/SrchQuickName.aspx")

# [Do whatever else is necessary...]

date_input = driver.find_element_by_id('ctl00_cphMain_SrchDates1_txtFiledFrom')
date_input.click()                      # Focus input field
date_input.send_keys(Keys.CONTROL, "a") # Select all pre-existing text/input value
date_input.send_keys(Keys.BACKSPACE)    # Remove that text
date_input.send_keys("01012011")        # Add desired text/set input value

发生了什么

这是打开到您的页面的 devtools 的屏幕截图:

devtools screenshot

有两点很突出:

  1. inputvalue="From"。因此,如果您立即调用 send_keys,输入的值为 From01012011
  2. 顺便说一下,他们似乎将 input 设置为具有 type=text(它实际上应该是 type=date!)

所以我们只需要 ctrl-a, backspace 来清除值。

答案 2 :(得分:-1)

我在Java中使用Actions类。 成功运行的Java代码如下:

package stackoverflow;

public class question1 {

@Test
public void a () throws InterruptedException{

    System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\CP-SAT\\Chromedriver\\chromedriver.exe");
    WebDriver a = new ChromeDriver();
    a.manage().window().maximize();
    a.get("https://cotthosting.com/NYRocklandExternal/LandRecords/protected/SrchQuickName.aspx");
    Thread.sleep(2000L);
    a.findElement(By.id("ctl00_cphMain_blkLogin_btnGuestLogin")).click();
    Thread.sleep(2000L);
    a.findElement(By.id("ctl00_cphMain_SrchDates1_txtFiledFrom")).click();
    Actions b = new Actions(a);
    for (int i = 0;i<6 ;i++){
        for(int j = 0; j<6; j++){   
                WebElement c = a.findElement(By.xpath("//*[@id='ctl00_cphMain_SrchDates1_ceFiledFrom_day_"+ i + "_" + j + "']"));
                Thread.sleep(2000L);
                b.moveToElement(c).build().perform();
            }
        }
    }
}

我试图在python中为你转换它,但我不确定语法。 以下是代码:

    a.get("https://cotthosting.com/NYRocklandExternal/LandRecords/protected/SrchQuickName.aspx");
    a.implicitly_wait(3);
    a.find_element_by_id("ctl00_cphMain_blkLogin_btnGuestLogin").click();
    a.implicitly_wait(3);
    a.find_element_by_id("ctl00_cphMain_SrchDates1_txtFiledFrom").click();
    actions = ActionChains(a);
    for (int i = 0;i<6 ;i++){
        for(int j = 0; j<6; j++){   
                WebElement c = a.find_element_by_xpath("//*[@id='ctl00_cphMain_SrchDates1_ceFiledFrom_day_"+ i + "_" + j + "']");
                a.implicitly_wait(3);
                actions.move_to_element(c).perform();

            }
        }

在您的最后尝试,让我知道更多问题。 快乐学习: - )

相关问题