You should instantiate a second driver for the second user session:
driver1 = webdriver.Chrome()
driver2 = webdriver.Chrome()
All of the windows for a particular driver instance share the same cookies; therefore, you can’t open a new window and expect to create a second user session. The second window will “see” the same cookies as the first, causing them to be sent to your website, wherein your app will see the visitor as already logged in under the first user session.
Creating a second driver instance gives you a second, isolated pool of cookies (as well as local and session storage), allowing you to safely and reliably create a second user session.
While you may have found a way to open an incognito/private window within the same driver instance, this is not supported by the WebDriver specification (on which Selenium is built) and thus is unlikely to be portable across browsers and platforms.
capybara-py makes this easy—transparently instantiating a new driver instance as needed:
import capybara
def test_concurrent_editors(page):
page.visit("/")
page.click_link("Sign In")
page.fill_in("Username", value="alice@example.com")
page.fill_in("Password", value="s33krit")
page.click_button("Sign In")
# ... do things with the default session
with capybara.using_session("other user"):
page.visit("/")
page.click_link("Sign In")
page.fill_in("Username", value="bob@example.com")
page.fill_in("Password", value="p@ssw0rd")
page.click_button("Sign In")
# ... do things with the "other user" session
# .... do more things with the default session
with capybara.using_session("other user"):
# ... do more things with the "other user" session
(Note: It comes with a pytest plugin that exposes the page
fixture and resets all sessions in between each test.)