3 回答

TA貢獻2019條經(jīng)驗 獲得超9個贊
是的,這是可能的。
要切換窗口,請.switch_to.window改為使用.swith_to_window:
driver.switch_to.window(driver.window_handles[1])
print(driver.current_window_handle)
[1]> 請?zhí)鎿Q為您想要的索引。
要獲得標(biāo)題使用:
print(driver.title)
要獲取索引當(dāng)前選項卡,請使用:
print(driver.window_handles.index(driver.current_window_handle))
或者for each像下面的例子一樣使用循環(huán)(參考你的場景):
driver.get('https://google.com')
driver.execute_script('window.open("https://yahoo.com");')
driver.execute_script('window.open("https://gmail.com");')
for handle in driver.window_handles:
driver.switch_to.window(handle)
print(driver.window_handles.index(handle))
print(handle)
print(driver.title)

TA貢獻1830條經(jīng)驗 獲得超9個贊
我很好奇為什么不直接切換到標(biāo)簽,因為你可能知道 id
它會比循環(huán)快得多
driver.switch_to_window(句柄)
添加回答
舉報