简体   繁体   中英

How to open new website via selenium and than open new tab and close new tab and work with first tab in python

i tried to work with selenium web driver for this plan " i open new link via selenium web driver on example website called scene-rls and do some work in this site and after i open new tab to open google.com then do some works and close the google tab and back to scene-rls tab and do new work on the scene-rls tab , but in my code when i open google.com and close the tab and back to the scene-rls tab the code return error like this:

selenium.common.exceptions.NoSuchWindowException: Message: no such window: window was already closed

and my code is :

  Regular_part = "Ambulance Australia S02E05 WEB H264-FLX"
  driver = 
webdriver.Chrome(executable_path=r'C://chromdriver//chromedriver.exe')

x = True
while x:
    driver.get('http://scene-rls.net/?s='+Regular_part+'&submit=Find')

    time.sleep(5)
    driver.find_element_by_xpath('/html/body/div/div[1]/div[2]/h1/a').click()

    print("Opent new tab")
    driver.execute_script("window.open ('https://google.com', 'new window')")

    time.sleep(3)
    print("back scn tab")

    driver.switch_to_window(driver.window_handles[0])

    time.sleep(3)
    print("back google tab")

    driver.switch_to_window(driver.window_handles[1])
    driver.execute_script("window.close ('https://google.com', 'new window')")


    z = input(":::")
    if z == "y":
        x = True
    else:
        x = False

After closing to window you have to Switch back to previous window again.

driver.execute_script("window.close ('https://google.com', 'new window')")
driver.switch_to.window(driver.window_handles[0])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM