摘抄python selenium 窗口操作
Opens a new tab
self.driver.execute_script("window.open()")
Switch to the newly opened tab
self.driver.switch_to.window(self.driver.window_handles[1])
Navigate to new URL in new tab
self.driver.get("https://google.com")
Run other commands in the new tab here
然后,您可以按如下方式关闭原始选项卡
Switch to original tab
self.driver.switch_to.window(self.driver.window_handles[0])
Close original tab
self.driver.close()
Switch back to newly opened tab, which is now in position 0
self.driver.switch_to.window(self.driver.window_handles[0])
或者关闭新打开的标签
Close current tab
self.driver.close()
Switch back to original tab
self.driver.switch_to.window(self.driver.window_handles[0])