#-*- coding: utf-8 -*-
from selenium importwebdriverfrom selenium.webdriver.common.by importByfrom selenium.webdriver.common.keys importKeysfrom selenium.webdriver.support.ui importSelectfrom selenium.common.exceptions importNoSuchElementExceptionfrom selenium.common.exceptions importNoAlertPresentExceptionfrom selenium.common.exceptions importStaleElementReferenceExceptionfrom selenium.webdriver.common.action_chains importActionChainsimportunittest, time, re#class Test(unittest.TestCase):
classTest():#def setUp(self):
def __init__(self):
self.driver=webdriver.Chrome()#self.driver = webdriver.Firefox()
self.driver.set_window_size('1600', '900')
self.driver.implicitly_wait(30)
self.base_url= "https://url"self.verificationErrors=[]
self.accept_next_alert=Truedeftest_(self):
driver=self.driver
driver.get(self.base_url+ "/zxcz/login/doLogin.do")
driver.find_element_by_id("loginName").clear()
driver.find_element_by_id("loginName").send_keys("A-test")
driver.find_element_by_id("password").clear()
driver.find_element_by_id("password").send_keys("888888")
driver.find_element_by_id("passKey").clear()
driver.find_element_by_id("passKey").send_keys("0000")
driver.find_element_by_link_text(u"登录").click()
driver.find_element_by_link_text(u"规则管理").click()
time.sleep(2)#xpath: href #(水平参考)
#driver.find_element_by_xpath("//*[@class='add']").click()
#driver.find_element_by_css_selector("a.add").click()
#先跳转到iframe,这里有两个同名的iframe,无法使用id定位
frame1 = driver.find_element_by_xpath("//iframe[contains(@src,'/zxcz/login/goIndex.do?menuId=1')]")
driver.switch_to.frame(frame1)
frame2= driver.find_element_by_xpath("//iframe[contains(@src,'/zxcz/rulemgr/ruleIndex.do')]")
driver.switch_to.frame(frame2)#点击添加按钮
driver.find_element_by_xpath("//a[contains(@href,'toSaveRule.do')]").click()#driver.find_element_by_xpath("//*[@id='saper-bd']/div/div[2]/div[1]/a[1]").click()
#js_start_time = "$('input[id=startTime]').attr('readonly','')"
#js_end_time = "$('input[id=endTime]').attr('readonly','')"
#action_input_time = ActionChains(driver)
try:#driver.find_element_by_id("taskName").clear()
driver.find_element_by_id("taskName").send_keys("0323-2")exceptException as e:print("error for finding element %s" %e)print("refind")
driver.find_element_by_id("taskName").clear()
driver.find_element_by_id("taskName").send_keys("0323-2")#action_input_time.move_to_element(end_filed).click().send_keys('2018-11-11 11:11:11').perform()
#driver.find_element_by_id('endTime').click()
#输入时间
js_remove_r_start_time = "document.getElementById('startTime').removeAttribute('readOnly')"js_remove_e_end_time= "document.getElementById('endTime').removeAttribute('readOnly')"driver.execute_script(js_remove_r_start_time)
driver.execute_script(js_remove_e_end_time)
js_input_s_value= "document.getElementById('startTime').value=('2018-01-01 12:12:01')"driver.execute_script(js_input_s_value)
js_input_e_value= "document.getElementById('endTime').value=('2019-01-01 12:12:01')"driver.execute_script(js_input_e_value)#time.sleep(2)
#开始时间
#start_filed = driver.find_element_by_id('startTime')
#driver.find_element_by_id('startTime').clear()
#driver.find_element_by_id('startTime').send_keys("2018-01-01 12:12:01")
#driver.implicitly_wait(2)
#结束时间
#driver.find_element_by_id('endTime').clear()
#driver.find_element_by_id('endTime').send_keys("2019-01-01 12:12:01")
#driver.implicitly_wait(2)
#跳转到第三个iframe
#frame3 = driver.find_element_by_xpath("//iframe[@src='about:blank']")
#driver.switch_to.frame(frame3)
#end_filed = driver.find_element_by_id('endTime')
#driver.find_element_by_id('startTime').send_keys("2018-02-01 11:13:01")
driver.implicitly_wait(2)
print_start_time= driver.find_element_by_id('startTime').get_attribute('value')
print_end_time= driver.find_element_by_id('endTime').get_attribute('value')print("print start time%s" %print_start_time)print("print end time%s" %print_end_time)#driver.find_element_by_id("startTime").click()
## ERROR: Caught exception [ERROR: Unsupported command [selectWindow | null | ]]
#driver.find_element_by_id("dpOkInput").click()
#driver.find_element_by_css_selector("td.WwdayOn").click()
#driver.find_element_by_id("dpOkInput").click()
## ERROR: Caught exception [ERROR: Unsupported command [selectWindow | null | ]]
#点击右边空白位置取消时间选择框
#action = ActionChains(driver)
#action.move_by_offset(0, 150).perform()
driver.implicitly_wait(3)
driver.find_element_by_xpath("//input[@value='doc' and @name='filesType' and @type='checkbox']").click()#点解可以@甘多样野,input係动作抑或属性
driver.find_element_by_id("files").clear()
driver.find_element_by_id("files").send_keys("C:\\Users\\Administrator\\Pictures\\333.png")
driver.find_element_by_name("remarks").clear()
driver.find_element_by_name("remarks").send_keys("0323")
Select(driver.find_element_by_id("uploadId")).select_by_visible_text("yj2")
driver.find_element_by_css_selector("input.wi100.submit").click()
driver.find_element_by_link_text(u"查阅").click()
driver.find_element_by_css_selector("input.wi100").click()defis_element_present(self, how, what):try:
self.driver.find_element(by=how, value=what)exceptNoSuchElementException as e:returnFalsereturnTruedefis_alert_present(self):try:
self.driver.switch_to_alert()exceptNoAlertPresentException as e:returnFalsereturnTruedefclose_alert_and_get_its_text(self):try:
alert=self.driver.switch_to_alert()
alert_text=alert.textifself.accept_next_alert:
alert.accept()else:
alert.dismiss()returnalert_textfinally:
self.accept_next_alert=True#def tearDown(self):
#self.driver.quit()
#self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":#unittest.main()
op=Test()
op.test_()