如何通过selenium和打开新网站,然后打开新选项卡和关闭新选项卡,并使用python中的第一个选项卡

如何通过selenium和打开新网站,然后打开新选项卡和关闭新选项卡,并使用python中的第一个选项卡,python,selenium,selenium-webdriver,webdriver,selenium-chromedriver,Python,Selenium,Selenium Webdriver,Webdriver,Selenium Chromedriver,我尝试使用selenium web驱动程序来实现此计划“我在名为scene rls的示例网站上通过selenium web driver打开新链接,在该网站上做一些工作,然后打开新选项卡打开google.com,然后做一些工作,关闭google选项卡,返回scene rls选项卡,在scene rls选项卡上做新工作,但是在我的代码中,当我打开google.com并关闭选项卡,然后返回到场景rls选项卡时,代码返回错误如下: selenium.common.exceptions.NoSuchWi

我尝试使用selenium web驱动程序来实现此计划“我在名为
scene rls
的示例网站上通过selenium web driver打开新链接,在该网站上做一些工作,然后打开新选项卡打开
google.com
,然后做一些工作,关闭
google
选项卡,返回
scene rls
选项卡,在
scene rls
选项卡上做新工作,但是在我的代码中,当我打开
google.com
并关闭选项卡,然后返回到
场景rls
选项卡时,代码返回错误如下:

selenium.common.exceptions.NoSuchWindowException:消息:没有这样的窗口:窗口已关闭

我的代码是:

  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])

无需使用JSE关闭选项卡。当上下文位于该选项卡中时,请使用
drvier.close()
关闭该选项卡。dud您能帮我解决此错误吗<代码>selenium.common.exceptions.WebDriverException:消息:未知错误:Runtime.evaluate抛出异常:SyntaxError:missing)在参数列表之后我想用变量代替链接代码是
xx=”http://scene-rls.net/?s=“+Regular\u part+”&submit=Find“driver.execute\u脚本(“window.open”(“+xx+”,“new window”)”)
试试看<代码>xx=”http://scene-rls.net/?s=“+Regular\u part+”&submit=Find”驱动程序。执行脚本(“”“window.open”(“{},_blank”);“”。格式(xx))