【问题标题】:Automate filling in date using selenium使用 selenium 自动填写日期
【发布时间】:2018-10-18 09:36:13
【问题描述】:

我正在尝试使用 selenium 填写以下网站上的表格。 https://bookings.doc.govt.nz/Saturn/Facilities/SearchViewGW.aspx

但是我正在努力设置日期(甚至让日期选择器出现)。

通过使用代码,我可以看到该值存储在那里:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
browser = webdriver.Firefox()
browser.get('https://bookings.doc.govt.nz/Saturn/Facilities/SearchViewGW.aspx')

el = browser.find_element_by_id('mainContent_homeContent_txtArrivalDate')
el.get_property('value')
el.clear()
el.send_keys("21122018")

但是,清除日期并发送新密钥会导致错误弹出窗口。 关于需要点击什么元素来调出日期选择器,或者如何直接设置所需日期的任何建议?

【问题讨论】:

    标签: python html selenium


    【解决方案1】:

    试试这个,只需使用 JavaScript 设置元素的 value 属性。 如果将 time.sleep 替换为更好的方法(等待元素可用),这可能不是您正在寻找的方法。

    我已经硬编码了日期 20/10/2018

    from selenium.webdriver import Chrome
    
    driver = Chrome()
    driver.get('https://bookings.doc.govt.nz/Saturn/Facilities/SearchViewGW.aspx')
    
    import time
    time.sleep(5)
    
    driver.execute_script(
       "document.getElementById(
           'mainContent_homeContent_txtArrivalDate').setAttribute('value', '20/10/2018')"
    )
    

    【讨论】:

      【解决方案2】:

      您引用的字段不允许您单击它并输入数字,因此您需要采取另一种方法。

      在此处查看本指南:https://www.guru99.com/handling-date-time-picker-using-selenium.html

      提供的代码是 Java,但 Selenium API 应该与您需要采用的方法相似。

      总而言之,您需要编写这样的代码(从文章中复制):

          //DAte and Time to be set in textbox
      
          String dateTime ="12/07/2014 2:00 PM";
      
          WebDriver driver = new FirefoxDriver();
      
          driver.manage().window().maximize();
      
          driver.get("http://demos.telerik.com/kendo-ui/datetimepicker/index");
      
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      
          //button to open calendar
      
          WebElement selectDate = driver.findElement(By.xpath("//span[@aria-controls='datetimepicker_dateview']"));
      
      selectDate.click();
      
      //button to move next in calendar
      
      WebElement nextLink = driver.findElement(By.xpath("//div[@id='datetimepicker_dateview']//div[@class='k-header']//a[contains(@class,'k-nav-next')]"));
      
      //button to click in center of calendar header
      
      WebElement midLink = driver.findElement(By.xpath("//div[@id='datetimepicker_dateview']//div[@class='k-header']//a[contains(@class,'k-nav-fast')]"));
      
      //button to move previous month in calendar
      
      WebElement previousLink = driver.findElement(By.xpath("//div[@id='datetimepicker_dateview']//div[@class='k-header']//a[contains(@class,'k-nav-prev')]")); 
      
          //Split the date time to get only the date part
      
          String date_dd_MM_yyyy[] = (dateTime.split(" ")[0]).split("/");
      
          //get the year difference between current year and year to set in calander
      
          int yearDiff = Integer.parseInt(date_dd_MM_yyyy[2])- Calendar.getInstance().get(Calendar.YEAR);
      
          midLink.click();
      
          if(yearDiff!=0){
      
              //if you have to move next year
      
              if(yearDiff>0){
      
                  for(int i=0;i< yearDiff;i++){
      
                      System.out.println("Year Diff->"+i);
      
                      nextLink.click();
      
                  }
      
              }
      
              //if you have to move previous year
      
              else if(yearDiff<0){
      
                  for(int i=0;i< (yearDiff*(-1));i++){
      
                      System.out.println("Year Diff->"+i);
      
                      previousLink.click();
      
                  }
      
              }
      
          }
      
          Thread.sleep(1000);
      
          //Get all months from calendar to select correct one
      
          List<WebElement> list_AllMonthToBook = driver.findElements(By.xpath("//div[@id='datetimepicker_dateview']//table//tbody//td[not(contains(@class,'k-other-month'))]"));
      
          list_AllMonthToBook.get(Integer.parseInt(date_dd_MM_yyyy[1])-1).click();
      
          Thread.sleep(1000);
      
          //get all dates from calendar to select correct one
      
          List<WebElement> list_AllDateToBook = driver.findElements(By.xpath("//div[@id='datetimepicker_dateview']//table//tbody//td[not(contains(@class,'k-other-month'))]"));
      
          list_AllDateToBook.get(Integer.parseInt(date_dd_MM_yyyy[0])-1).click();
      
          ///FOR TIME
      
          WebElement selectTime = driver.findElement(By.xpath("//span[@aria-controls='datetimepicker_timeview']"));
      
          //click time picker button
      
          selectTime.click();
      
          //get list of times
      
          List<WebElement> allTime = driver.findElements(By.xpath("//div[@data-role='popup'][contains(@style,'display: block')]//ul//li[@role='option']"));
      
          dateTime = dateTime.split(" ")[1]+" "+dateTime.split(" ")[2];
      
       //select correct time
      
          for (WebElement webElement : allTime) {
      
              if(webElement.getText().equalsIgnoreCase(dateTime))
      
              {
      
                  webElement.click();
      
              }
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-29
        • 2016-07-24
        • 2013-07-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多