如何使用 selenium 打开和管理新标签页
How to open and manage new tabs with selenium
我正在尝试使用 'https://www.gmail.com' url 打开一个新标签页,然后选择一些信息并 return 到第一页,我正在使用 CTRL + t命令打开新标签页,但是,如何在两个页面之间切换?
我的代码片段:
driver.find_elements_by_css_selector('body').send_keys(Keys.CONTROL + "t")
实际代码:
bot = self.bot
guids = bot.window_handles()
time.sleep(2)
bot.execute_script("window.open()")
bot.get('https://gmail.com')
time.sleep(1)
Python版本=Python3.8.5
#current window
first_tab = bot.window_handles[0]
#create new tab
bot.execute_script("window.open()")
#move to new tab
new_tab = bot.window_handles[1]
bot.switch_to.window(new_tab)
bot.get('https://gmail.com')
#switch to first tab
bot.switch_to.window(first_tab)
我正在尝试使用 'https://www.gmail.com' url 打开一个新标签页,然后选择一些信息并 return 到第一页,我正在使用 CTRL + t命令打开新标签页,但是,如何在两个页面之间切换? 我的代码片段:
driver.find_elements_by_css_selector('body').send_keys(Keys.CONTROL + "t")
实际代码:
bot = self.bot
guids = bot.window_handles()
time.sleep(2)
bot.execute_script("window.open()")
bot.get('https://gmail.com')
time.sleep(1)
Python版本=Python3.8.5
#current window
first_tab = bot.window_handles[0]
#create new tab
bot.execute_script("window.open()")
#move to new tab
new_tab = bot.window_handles[1]
bot.switch_to.window(new_tab)
bot.get('https://gmail.com')
#switch to first tab
bot.switch_to.window(first_tab)