• python webdriver api-操作日期元素的方法


    操作日期元素

    第一种方式直接向输入框输入日期

    dateInputBox = 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):

            # 启动Chrome浏览器

            #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"

            # 访问指定的网址

            self.driver.get(url)

            try:

                # 创建一个显示等待对象

                wait = WebDriverWait(self.driver, 10, 0.2)

                # 显示等待判断被测试页面上的日期输入框是否可见并且能被点击

                wait.until(EC.element_to_be_clickable((By.ID, 'datepicker')))

            except TimeoutException, e:

                # 捕获TimeoutException异常

                print traceback.print_exc()

            except NoSuchElementException, e:

                # 捕获NoSuchElementException异常

                print traceback.print_exc()

            except Exception, 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):

            # 退出IE浏览器

            self.driver.quit()

    if __name__ == '__main__':

        unittest.main()

    D: est>python test.py

    .

    ----------------------------------------------------------------------

    Ran 1 test in 31.638s

    OK

    第二种方式点选,找到某个日期,直接选

    dateInputBox = 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()
    如果想跨天可以点击下边元素试试

    //*[@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):

            # 启动Chrome浏览器

            self.driver = webdriver.Firefox(executable_path = "d:\geckodriver")

        def test_datePicker(self):

            url = "http://jqueryui.com/resources/demos/datepicker/other-months.html"

            # 访问指定的网址

            self.driver.get(url)

            try:

                # 创建一个显示等待对象

                wait = WebDriverWait(self.driver, 10, 0.2)

                # 显示等待判断被测试页面上的日期输入框是否可见并且能被点击

                wait.until(EC.element_to_be_clickable((By.ID, 'datepicker')))

            except TimeoutException, e:

                # 捕获TimeoutException异常

                print traceback.print_exc()

            except NoSuchElementException, e:

                # 捕获NoSuchElementException异常

                print traceback.print_exc()

            except Exception, e:

                # 捕获其他异常

                print traceback.print_exc()

            else:

                # 查找被测试页面上的日期输入框页面元素

                dateInputBox = self.driver.find_element_by_id("datepicker")

                # 查找到日期输入框,直接输入指定格式的日期字符串

                # 就可以变相模拟在日期控件上进行选择了

                # 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):

            # 退出IE浏览器

            self.driver.quit()

    if __name__ == '__main__':

        unittest.main()

    D: est>python test.py

    .

    ----------------------------------------------------------------------

    Ran 1 test in 32.865s

    OK

  • 相关阅读:
    Atitit。D&D drag&drop拖拽功能c#.net java swing的对比与实现总结
    Atitit . 编程模型的变革总结
    Atitit.mysql oracle with as模式临时表模式 CTE 语句的使用,减少子查询的结构性 mssql sql server..
    Atitit.编程语言的主要的种类and趋势 逻辑式语言..函数式语言...命令式语言
    atitit.编程语言会形成进化树--哪些特性会繁荣??通才还是专才的选型 现代编程语言的特性总结
    Atitit.多媒体区----web视频格式的选择总结
    Atitit.web 视频播放器classid clsid 大总结quicktime,vlc 1. Classid的用处。用来指定播放器 1 2. <object> 标签用于包含对象,比如图像、音
    Atitit.部分错误 设计模式,异常处理框架atiPartErr 的总结
    Atitit.js javascript异常处理机制与java异常的转换 多重catc hDWR 环境 .js exception process Vob7
    atitit.判断时间重叠方法总结 java c++ c#.net js php
  • 原文地址:https://www.cnblogs.com/xiaxiaoxu/p/9203256.html
Copyright © 2020-2023  润新知