When I open the URL with driver.get(url)
, how can I get the response content of the page? Please refer to the image for more information.
Asked
Active
Viewed 1.1k times
1

Mahipal
- 900
- 1
- 5
- 7

Alex Bruce
- 533
- 2
- 10
- 23
-
Possible duplicate of [How to get HTTP Response Code using Selenium WebDriver with Java?](http://stackoverflow.com/questions/6509628/how-to-get-http-response-code-using-selenium-webdriver-with-java) – Tom May 10 '17 at 03:28
-
2my question is different with that one – Alex Bruce May 10 '17 at 05:15
-
Can you please elaborate what is your exact testing step? What worked for you? Show code. Share the relevant HTML DOM as well. Thanks – undetected Selenium May 10 '17 at 05:57
-
@Dev There is a token in this page,I want to get the token when I open this page with driver.get(url), then the token text will be used for login – Alex Bruce May 10 '17 at 06:04
-
How does pattern of the token looks like? e.g. Any matching patterns? – undetected Selenium May 10 '17 at 06:07
2 Answers
-1
I'm using Python and Django, but it's actually simple to get the response. I'm using a StaticLiveServerTestCase
as my base test for the test. The .get()
method on self.client
actually returns the response itself. For example:
response = self.client.get(url)
However, it looks like what you're really trying to get is the cookie based on what you're pointing to in the picture. I use Django and the Django test suite to authenticate a user session to be used in the test.
def create_pre_authenticated_session(self, username, url="/"):
user = User.objects.create(username=username)
session = SessionStore()
session[SESSION_KEY] = user.pk
session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0]
session[HASH_SESSION_KEY] = user.get_session_auth_hash()
session.save()
# to set a cookie we need to first visit the domain.
# 404 pages load the quickest!
self.browser.get(self.live_server_url + '/404_no_such_url/')
self.browser.add_cookie(dict(
name=settings.SESSION_COOKIE_NAME,
value=session.session_key,
secure=False,
path='/',
))
self.browser.get(self.live_server_url + url)
return user
This has some other stuff in it that I borrowed from Percival's Test-Driven Development with Python, but I hope that it can provide some guidance on what you're trying to accomplish.

Bobort
- 3,085
- 32
- 43
-
-
@BryanGreen, I have fixed my response. It turns out I was using `StaticLiveServerTestCase` which has its own `self.client` that returns the response from the browser. – Bobort Apr 29 '22 at 15:50