标签:
核心代码
js ="setTimeout(function(){document.getElementsByTagName(‘Button‘)[3].click()},100);"
1 #-*- coding: utf-8 -*- 2 __author__ = ‘ray‘ 3 from selenium import webdriver 4 import unittest 5 import time 6 7 class modolwindowdemo(unittest.TestCase): 8 def setUp(self): 9 self.driver = webdriver.Firefox() 10 self.driver.implicitly_wait(30) 11 self.base_url = "http://pyselenium-d1826.coding.io/exapage.html" 12 self.verificationErrors = [] 13 self.js = "setTimeout(function(){document.getElementsByTagName(‘Button‘)[3].click()},100);" 14 15 16 def test_demo(self): 17 driver = self.driver 18 driver.get(self.base_url) 19 #获取当前窗口句柄 20 mainhandle = driver.current_window_handle 21 print mainhandle 22 #使用下面的webdriver打开模态窗口,无法进行切换 23 #driver.find_element_by_xpath("//button[2]").click() 24 25 #使用js异步进行打开模态窗口 26 driver.execute_script(self.js) 27 time.sleep(1) 28 #获取当前所有句柄 29 modalhandle = driver.window_handles 30 31 #通过循环切换到模态窗口 32 print modalhandle 33 for handle in modalhandle: 34 if handle != mainhandle: 35 driver.switch_to_window(handle) 36 print driver.current_window_handle 37 driver.find_element_by_xpath("//form//tr[1]/td[2]/input").send_keys("Ray") 38 39 time.sleep(2) 40 def tearDown(self): 41 self.driver.quit() 42 self.assertEqual([], self.verificationErrors) 43 44 if __name__ == "__main__": 45 unittest.main()
标签:
原文地址:http://www.cnblogs.com/tsbc/p/4273538.html