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

來源:互聯網
上載者:User

標籤:__name__   --   test   exce   .com   exe   stc   print   div   

操作日期元素

 

第一種方式直接向輸入框輸入日期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:\test>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:\test>python test.py

.

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

Ran 1 test in 32.865s

 

OK

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.