我想使用Selenium在我的驱动程序上执行以下代码:
driver.execute_script("window.open('{0}', '_blank');".format(player_url))
然而,与此同时,我想跟踪它打开的标签。我要同时打开这么多标签,很难跟踪它在我手中打开的标签。但是driver.execute_script()
并没有让我知道。其他功能也试图打开一个新窗口。
那么有可能知道它打开了哪个标签吗?
答案 0 :(得分:2)
您可以实现以下内容:
players = [player_url_1, player_url_2, player_url_3]
windows = {}
current = driver.current_window_handle
for player_url in players:
driver.execute_script("window.open('{0}', '_blank');".format(player_url))
new_window = [window for window in driver.window_handles if window != current and window not in windows.values()][0]
windows[player_url] = new_window
打开所有窗口后,windows
应如下所示:
{player_url_1: 'CDwindow-XXX', player_url_2: 'CDwindow-YYY', player_url_3: 'CDwindow-ZZZ'}
所以你得到了" URL_of_page" 的词典:" ID_of_tab" 对