我有一個鏈接串列(url),我需要每個鏈接在不同的選項卡中打開,例如:
- youtube.com
- google.com
- 臉書網
該腳本將打開 youtube,然后在另一個視窗中,它將打開 google,但我需要關閉 youtube 標簽,然后我需要打開 facebook 標簽。
My code is:
if(posts!=len(data)-1):
driver.execute_script("window.open('');")
chwd = driver.window_handles
driver.switch_to.window(chwd[-1])
driver.close()
driver.switch_to.window(chwd[-2])
driver.quit()
uj5u.com熱心網友回復:
我假設您之前只有一個打開的視窗driver.execute_script("window.open('');")。
嘗試
if(posts!=len(data)-1):
old_window = driver.window_handles[0]
driver.execute_script("window.open('');")
driver.switch_to.window(old_window)
driver.close()
driver.switch_to.default_content()
driver.quit()
uj5u.com熱心網友回復:
wait=WebDriverWait(driver,60)
urls=["https://www.youtube.com/","https://www.google.com/"]
chwd = driver.window_handles
for url in urls:
old = driver.current_window_handle
driver.execute_script("window.open('{arguements[0]');",url)
wait.until(EC.number_of_windows_to_be(len(chwd) 1))
driver.switch_to.window(old)
driver.close()
driver.switch_to.default_content()
driver.quit()
我會這樣做以在視窗打開后立即關閉它,這樣您就不會意外關閉東西。然后切換回默認值。
進口:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/416037.html
標籤:
上一篇:無法在選項卡下抓取影像
