Bro I really can't explain it without you having the fileissue related to locators or waits ?
You’re basically describing what Selenium does out of the box.
It can open a browser, perform actions, and close the tab automatically.
Check the official docs — everything’s well explained with examples:
https://www.selenium.dev/documentation/