Skip to content

Instantly share code, notes, and snippets.

@AutomatedTester
Created February 17, 2011 15:46
Show Gist options
  • Select an option

  • Save AutomatedTester/831946 to your computer and use it in GitHub Desktop.

Select an option

Save AutomatedTester/831946 to your computer and use it in GitHub Desktop.
from selenium import webdriver
from selenium.webdriver.common.by import By
import os
mainhtml = """
<!DOCTYPE HTML>
<html>
<head>
<title>window switching</title>
</head>
<body>
<p>
Click <a id="open" href="#" onclick='window.open("closeable.html")'>here</a> to open a new window.
</p>
</body>
</html>
"""
closeable = """
<!DOCTYPE HTML>
<html>
<head>
<title>closeable window</title>
</head>
<body>
<p>
Click <a id="close" href="#" onclick="window.close()">here</a> to close this window.
</p>
</body>
</html>
"""
mydir = os.path.join(os.path.dirname(__file__))
def main():
f1 = open(mydir+"/windows.html", "w")
f1.write(mainhtml)
f1.close()
f2 = open(mydir+"/closeable.html", "w")
f2.write(closeable)
f2.close()
for i in range(20):
run_test()
os.remove(mydir+"/windows.html")
os.remove(mydir+"/closeable.html")
def run_test():
driver = webdriver.Ie()
driver.get("file://" + mydir + "/windows.html")
driver.implicitly_wait(10)
driver.find_element_by_id("open").click()
orig = driver.get_current_window_handle()
handles = driver.get_window_handles()
result = False
for handle in handles:
driver.switch_to_window(handle)
result = driver.title == "closeable window"
driver.switch_to_window(orig)
if result:
break
if result == False:
raise Exception("not found")
driver.switch_to_window(handle)
driver.find_element_by_id("close").click()
driver.switch_to_window(orig)
driver.quit()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment