Python 3.5; Django 1.10.7 I am struggling to get access to the admin pages via Selenium.
I assume the issue is with the user login not being set properly - I don't want to go via the login page/form every time, so was attempting the session approach. Below is the code that shows the problem. The last few lines are attempting to access a known element (appears on any admin page); but the actual page that gets displayed is one you would see if you were not logged in. Any hints on how to make this work would be appreciated. # -*- coding: utf-8 -*- """ Purpose: Test admin list display """ # lib import os # third party from selenium import webdriver # django from django.conf import settings from django.test import LiveServerTestCase from django.contrib.auth import get_user_model, SESSION_KEY, \ BACKEND_SESSION_KEY, HASH_SESSION_KEY from django.contrib.sessions.backends.db import SessionStore # project from users.models import CustomUser as User os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = 'localhost:8007' class LoginSeleniumTest(LiveServerTestCase): """selenium-based test""" def setUp(self): # setup browser if settings.USE_CHROME: self.browser = webdriver.Chrome(settings.CHROME_DRIVER) elif settings.USE_FF: self.browser = webdriver.Firefox() else: raise NotImplementedError('Unknown browser for Selenium tests') # create user self.email = 'f...@test.com' user = User( is_staff=True, is_active=True, is_superuser=True, email=self.email, full_name="Foo Bar", nick_name="Foo") user.set_password('foobar') user.save() super(LoginSeleniumTest, self).setUp() def test_login_and_access_admin(self): # create fake session with real user user = get_user_model().objects.get(email=self.email) self.session = SessionStore() self.session[SESSION_KEY] = user.pk self.session[BACKEND_SESSION_KEY] = 'django.contrib.auth.backends.ModelBackend' self.session[HASH_SESSION_KEY] = user.get_session_auth_hash() self.session.save() print("HASH_KEY:\n", self.session[HASH_SESSION_KEY]) # set cookie based on current page domain self.browser.get(self.live_server_url + '/dummy/') self.browser.add_cookie( {'name': settings.SESSION_COOKIE_NAME, 'value': self.session.session_key, 'secure': False, 'path': '/'} ) # access an admin (password-protected) page page_name = '/admin/' print("URL:\n", self.live_server_url + page_name) self.browser.get(self.live_server_url + page_name) print("PAGE:\n", self.browser.page_source) # throws a selenium.common.exceptions.NoSuchElementException ...? result = self.browser.find_element_by_id('user-tools') -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAF1Wu3PtwvY6pNriiTTkYEws0Gg2gU90FQmkFC7Doc3TVHFg7Q%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.