Python Webdriver api-method for manipulating date elements

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.