#!usr/bin/env python
#-*- coding:utf-8 -*-
"""
@author: sleeping_cat
@Contact : zwy24zwy@163.com
"""
#操作日期控件
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):
self.driver = webdriver.Chrome()
def test_datePicker(self):
url = ‘http://jqueryui.com/resources/demos/datepicker/other-months.html‘
self.driver.get(url)
time.sleep(3)
try:
wait = WebDriverWait(self.driver,10,0.2)
wait.until(EC.element_to_be_clickable((By.ID,‘datepicker‘)))
except TimeoutException as e:
print(traceback.print_exc())
except NoSuchElementException as e:
print(traceback.print_exc())
except Exception as e:
print(traceback.print_exc())
else:
dateInputBox = self.driver.find_element_by_id(‘datepicker‘)
dateInputBox.send_keys(‘11/24/2016‘)
time.sleep(3)
def tearDown(self):
self.driver.quit()
if __name__ == ‘__main__‘:
unittest.main()
注意:该被测网址的日期控件支持输入,但有时会遇到日期选择控件不允许用户输入的情况,此种情况可通过JavaScript语句改变页面元素属性值的方式将日期控件修改成可编辑状态,以便完成脚本直接输入日期来进行日期选择。