日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

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

發(fā)布時(shí)間:2024/8/26 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python webdriver api-操作日期元素的方法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

操作日期元素

?

第一種方式直接向輸入框輸入日期

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

??????? # 啟動(dòng)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"

??????? # 訪問(wèn)指定的網(wǎng)址

??????? self.driver.get(url)

??????? try:

??????????? # 創(chuàng)建一個(gè)顯示等待對(duì)象

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

??????????? # 顯示等待判斷被測(cè)試頁(yè)面上的日期輸入框是否可見(jiàn)并且能被點(diǎn)擊

??????????? 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:

??????????? # 查找被測(cè)試頁(yè)面上的日期輸入框頁(yè)面元素

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

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

??????????? # 就可以變相模擬在日期控件上進(jìn)行選擇了

??????????? 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

第二種方式點(diǎn)選,找到某個(gè)日期,直接選

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()
如果想跨天可以點(diǎn)擊下邊元素試試

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

??????? # 啟動(dòng)Chrome瀏覽器

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

?

??? def test_datePicker(self):

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

??????? # 訪問(wèn)指定的網(wǎng)址

??????? self.driver.get(url)

??????? try:

??????????? # 創(chuàng)建一個(gè)顯示等待對(duì)象

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

??????????? # 顯示等待判斷被測(cè)試頁(yè)面上的日期輸入框是否可見(jiàn)并且能被點(diǎn)擊

??????????? 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:

??????????? # 查找被測(cè)試頁(yè)面上的日期輸入框頁(yè)面元素

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

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

??????????? # 就可以變相模擬在日期控件上進(jìn)行選擇了

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

?

轉(zhuǎn)載于:https://www.cnblogs.com/xiaxiaoxu/p/9203256.html

總結(jié)

以上是生活随笔為你收集整理的python webdriver api-操作日期元素的方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。