使用非同步js解決模態視窗切換的辦法

來源:互聯網
上載者:User

標籤:

核心代碼

  1. js ="setTimeout(function(){document.getElementsByTagName(‘Button‘)[3].click()},100);"
下面是我的實驗過的範例程式碼:實驗地址:http://pyselenium-d1826.coding.io/exapage.html
  1.  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.driver18         driver.get(self.base_url)19         #擷取當前視窗控制代碼20         mainhandle = driver.current_window_handle21         print mainhandle22         #使用下面的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_handles30 31         #通過迴圈切換到模態視窗32         print modalhandle33         for handle in modalhandle:34             if handle != mainhandle:35                 driver.switch_to_window(handle)36                 print driver.current_window_handle37                 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()

 

使用非同步js解決模態視窗切換的辦法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.