簡體   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