So I have the first tab; the main tab. I want to open multiple tabs using selenium and once their task is done, to close them until we're back to the original first tab
url = "https://www.google.com/"
i=4
for each in reversed(range(i, 0, -1)):
print(each)
driver.execute_script(f"window.open('{url}');")
driver.switch_to.window(driver.window_handles[each])
#do stuff
driver.close()
driver.switch_to.window(driver.window_handles[each-1])
I get the following error:
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed from unknown error: web view not found
I know its something to do with the index values of each tab, where 0 would be the inital tab, correct me if I'm wrong, but I can't get it to work.# properly.
The best I could do is to get it to close each tab randomly until left with 2, then suddenly it closes both of them (no tabs left, exits browser) when reach the value of 1