Action Date Element
the first way to enter a date directly into the input boxDateinputbox = self.driver.find_element_by_id ("DatePicker")Dateinputbox.send_keys ("11/24/2016")
#encoding =utf-8
From selenium import Webdriver
Import UnitTest, Time, Traceback
From Selenium.webdriver.support.ui import webdriverwait
From selenium.webdriver.common.by Import by
From Selenium.webdriver.support import expected_conditions as EC
From selenium.common.exceptions import TimeoutException, nosuchelementexception
Class Testdemo (UnitTest. TestCase):
def setUp (self):
# Launch the Chrome browser
#self. Driver = Webdriver. Ie (Executable_path = "E:\\iedriverserver")
Self.driver = Webdriver. Firefox (Executable_path = "D:\\geckodriver")
def test_datepicker (self):
url = "Http://jqueryui.com/resources/demos/datepicker/other-months.html"
# access to the specified URL
Self.driver.get (URL)
Try
# Create a display wait object
wait = webdriverwait (Self.driver, 10, 0.2)
# display wait to determine if the date input box on the tested page is visible and can be clicked
Wait.until (ec.element_to_be_clickable (by.id, ' DatePicker '))
Except TimeoutException, E:
# Capturing TimeoutException Exceptions
Print Traceback.print_exc ()
Except Nosuchelementexception, E:
# Capturing Nosuchelementexception Exceptions
Print Traceback.print_exc ()
Except Exception, E:
# Catch Other exceptions
Print Traceback.print_exc ()
Else
# Find the Date Input box page element on the tested page
Dateinputbox = self.driver.find_element_by_id ("DatePicker")
# Find the Date input box and enter the date string in the specified format directly
# You can make a selection on a date control in a disguised simulation
Dateinputbox.send_keys ("11/24/2016")
Time.sleep (3)
def tearDown (self):
# Quit IE browser
Self.driver.quit ()
if __name__ = = ' __main__ ':
Unittest.main ()
D:\test>python test.py
.
----------------------------------------------------------------------
Ran 1 Test in 31.638s
Ok
the second way to click, to find a date, directly selectDateinputbox = self.driver.find_element_by_id ("DatePicker")Dateinputbox.click ()Self.driver.find_element_by_xpath (".//*[@id = ' ui-datepicker-div ']/table/tbody/tr[2]/td[1]/a"). Click ()if you want to cross-day click on the elements below try
*[@id = ' Ui-datepicker-div ']/div/a[2]/span
#self. Driver = Webdriver. Firefox (Executable_path = "D:\\geckodriver")
#encoding =utf-8
From selenium import Webdriver
Import UnitTest, Time, Traceback
From Selenium.webdriver.support.ui import webdriverwait
From selenium.webdriver.common.by Import by
From Selenium.webdriver.support import expected_conditions as EC
From selenium.common.exceptions import TimeoutException, nosuchelementexception
Class Testdemo (UnitTest. TestCase):
def setUp (self):
# Launch the Chrome browser
Self.driver = Webdriver. Firefox (Executable_path = "D:\\geckodriver")
def test_datepicker (self):
url = "Http://jqueryui.com/resources/demos/datepicker/other-months.html"
# access to the specified URL
Self.driver.get (URL)
Try
# Create a display wait object
wait = webdriverwait (Self.driver, 10, 0.2)
# display wait to determine if the date input box on the tested page is visible and can be clicked
Wait.until (ec.element_to_be_clickable (by.id, ' DatePicker '))
Except TimeoutException, E:
# Capturing TimeoutException Exceptions
Print Traceback.print_exc ()
Except Nosuchelementexception, E:
# Capturing Nosuchelementexception Exceptions
Print Traceback.print_exc ()
Except Exception, E:
# Catch Other exceptions
Print Traceback.print_exc ()
Else
# Find the Date Input box page element on the tested page
Dateinputbox = self.driver.find_element_by_id ("DatePicker")
# Find the Date input box and enter the date string in the specified format directly
# You can make a selection on a date control in a disguised simulation
# Dateinputbox.send_keys ("11/24/2016") #直接输入的方式,
Dateinputbox.click ()
Time.sleep (1)
Self.driver.find_element_by_xpath (".//*[@id = ' ui-datepicker-div ']/table/tbody/tr[2]/td[1]/a"). Click ()
Time.sleep (3)
def tearDown (self):
# Quit IE browser
Self.driver.quit ()
if __name__ = = ' __main__ ':
Unittest.main ()
D:\test>python test.py
.
----------------------------------------------------------------------
Ran 1 Test in 32.865s
Ok
Python Webdriver api-method for manipulating date elements