嗨,我有一個在主選項卡中打開以接受 cookie 的主網頁,其余鏈接應在選項卡中回圈打開和關閉:
links = ['https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/']
driver = webdriver.Chrome(executable_path=r'C:\ProgramFiles(x86)\chromedriver.exe')
driver.get('https://www.deviceinfo.me/') #open the main tab
for link in links:
driver.execute_script("window.open();") # open a new tab
driver.switch_to.window(driver.window_handles[1]) # switch to the tab 1
driver.get(link)
driver.close() # close tab 1
但這不起作用,有什么建議可以解決這個問題嗎?
謝謝。
uj5u.com熱心網友回復:
這應該有效:
您基本上到了 window_handle[1] 但沒有切換回父視窗,因此出現了問題
links = ['https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/']
driver = webdriver.Chrome(executable_path=r'C:\ProgramFiles(x86)\chromedriver.exe')
driver.get('https://www.deviceinfo.me/') #open the main tab
for link in links:
driver.execute_script("window.open();") # open a new tab
driver.switch_to.window(driver.window_handles[1]) # switch to the tab 1
driver.get(link)
driver.close() # close tab 1
driver.switch_to.window(driver.window_handles[0])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/411717.html
標籤:
