繁体   English   中英

如何通过硒打开新网站,然后打开新标签页并关闭新标签页并使用python中的第一个标签页

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

我为此计划尝试使用硒网络驱动程序“我通过示例网站上的硒网络驱动程序打开了新链接,称为scene-rls ,并在此站点中进行了一些工作,在我打开新标签页以打开google.com之后,然后进行了一些工作,关闭google标签,然后返回scene-rls标签,并在scene-rls标签上进行新工作,但是在我的代码中,当我打开google.com并关闭该标签并返回到scene-rls标签时,代码返回这样的错误:

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

我的代码是:

  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

关闭窗口后,您必须再次切换回上一个窗口。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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