您可以使用window_handles
和switch_to_window
方法。
之前點擊該鏈接的窗口句柄作爲
window_before = driver.window_handles[0]
第一存儲點擊鏈接店新打開的窗口的窗口句柄作爲
window_after = driver.window_handles[1]
後,然後執行切換到窗口methow移動到新開的窗口
driver.switch_to_window(window_after)
與此類似,您可以切換下注新舊窗口。以下是代碼示例
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
class GoogleOrgSearch(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
def test_google_search_page(self):
driver = self.driver
driver.get("http://www.cdot.in")
window_before = driver.window_handles[0]
print window_before
driver.find_element_by_xpath("//a[@href='http://www.cdot.in/home.htm']").click()
window_after = driver.window_handles[1]
driver.switch_to_window(window_after)
print window_after
driver.find_element_by_link_text("ATM").click()
driver.switch_to_window(window_before)
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
按照你的代碼,當執行'window_after = browser.window_handles [1]'時,我收到一個'IndexError:列表索引超出範圍'。哪裏不對?請參閱:https://stackoverflow.com/questions/45354850/focus-on-second-page-form-with-selenium – LucSpan