Is it possible to continue a request session in selenium will all its cookies, i have seen many people doing it the other way arround but i have no clue how to do it proper the way i want.
def open_selenium_session(self):
# get or set cookies
driver = get_chromedriver(self.proxy, use_proxy=True, user_agent=True)
driver.get("https://www.instagram.com")
cookies = driver.get_cookies()
for cookie in cookies:
self.session.cookies.set(cookie['name'], cookie['value'])
Incase you have stored the cookies previesly from an active session using pickle:
import pickle
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
You can always set them back as follows:
# loading the stored cookies
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
# adding the cookies to the session through webdriver instance
driver.add_cookie(cookie)
Reference
You can find a couple of detailed discussions in:
org.openqa.selenium.InvalidCookieDomainException: Document is cookie-averse using Selenium and WebDriver
selenium.common.exceptions.InvalidCookieDomainException: Message: invalid cookie domain while executing tests in Django with Selenium
Related
I want to automate requests on a website and when doing so, I need a session cookie in order to identify myself.
When checking the network tab, I can clearly see the session cookie, but when checking the Application tab, this cookie is not shown. After accessing this website with selenium and calling driver.get_cookies() with Python, I only get the cookies shown in the Application tab.
I need to do this with selenium because this way it's possible to login. Only using requests will not work.
I do not have many clues on how to get that cookie and have almost 0 experience in this field, hence my question.
I am trying to use the login from selenium to use in the next http request in jmeter. I currently have the below
My selenium script works perfectly and logs me into my website as per the below
WDS.sampleResult.sampleStart()
WDS.browser.get('https://www.testwebsite.com')
WDS.browser.findElement(org.openqa.selenium.By.linkText("Login")).click();
WDS.browser.findElement(org.openqa.selenium.By.id("username")).sendKeys("myusername");
WDS.browser.findElement(org.openqa.selenium.By.id("password")).sendKeys("mypassword");
WDS.browser.findElement(org.openqa.selenium.By.xpath("//button[#type='submit']")).click();
java.lang.Thread.sleep(5000)
WDS.browser.findElement(org.openqa.selenium.By.xpath("//*[contains(text(),'Skip for now')]")).click();
var cookies = WDS.browser.manage().getCookies()
java.lang.Thread.sleep(5000)
WDS.sampleResult.sampleEnd()
What i want to do is keep this session alive and then on the next request use something like this
GET - https://test/anotherpage.com
so i want it to recognise im still logged in. How can i keep the session alive so my jmeter http request can work?
In your WebDriver Sampler you need to store the cookies object into JMeter Variables like:
WDS.vars.putObject('cookies', cookies)
Add HTTP Cookie Manager to your Test Plan
Add JSR223 PreProcessor as a child of the HTTP Request sampler where you need to get the authentication context and put the following code into "Script" area:
def cookies = vars.getObject('cookies')
log.info('cookies=' + cookies)
cookies.collect { cookie ->
new org.apache.jmeter.protocol.http.control.Cookie(cookie.getName(),
cookie.getValue(),
cookie.getDomain(),
cookie.getPath(),
cookie.isSecure(),
cookie.getExpiry().getTime())
}.each { cookie -> sampler.getCookieManager().add(cookie) }
This way you can copy the cookies from the browser to HTTP Request sampler so the request will be authenticated
More information: Modifying Cookies in JMeter with Groovy
Using cookies in selenium, which it looks like yuou've fetched all of them:
https://www.selenium.dev/documentation/en/support_packages/working_with_cookies/
YHou should be able to store those cookies, which should be enough to preserve your session.
https://www.blazemeter.com/blog/using-http-cookie-manager-jmeter
To save cookies as variables, define the property
"CookieManager.save.cookies=true". The names of the cookies contain
the prefix "COOKIE_" before they are stored (this avoids accidental
corruption of local variables). To revert to the original behavior,
define the property "CookieManager.name.prefix= " (with one or more
spaces). If enabled, the value of a cookie with the name TEST can be
referred to as ${COOKIE_TEST}.
That being said, you probably don't need to use selenium (and launch a full browser) to do the login, you could probably fire a simple http request to post the login form. This would be less over head
https://guide.blazemeter.com/hc/en-us/articles/207421705-How-to-use-JMeter-for-Login-Authentication-How-to-use-JMeter-for-Login-Authentication
I'm using Selenium on Python 3, to perform automated actions on an internal webpage on Chrome v81 browser, where google authentication (logging into gmail) is required.
Getting cookies (after manually logging in):
pickle.dump(driver.get_cookies(), open(path_to_cookie, 'wb'))
Using cookies:
driver.get(url)
cookies = pickle.load(open(path_to_cookie, "rb"))
for cookie in cookies:
if 'expiry' in cookie:
del cookie['expiry']
driver.add_cookie(cookie)
time.sleep(2)
driver.get(url)
Problem:
The cookies are stored in a pickle file. There are 2 types of cookies - csrftoken and sessionid.
The sessionid one typically expires in 2 weeks, and even if i change the expiry date of the cookies in the json (in the cookies.pkl file), it will still expire after 2 weeks (and when i run the script it shows the gmail login page).
I've tried using user-data-dir and chrome profile (a duplicate of my existing chrome profile). It works, but the same problem still happens and i need to relogin again after 2 weeks. Any idea how i can make the cookies last for e.g months / a year instead of 2 weeks?
I have an Express server using PassportJS authentication. I am using standard XMLHTTPRequests to retrieve files.
Chrome and Firefox work without problems. Safari (Mac and iOS) is failing to send a request cookie with some requests and doing this inconsistently.
Logged failed requests all have a different session id that isn't authenticated with PassportJS.
Chrome and Firefox have a single session id for all requests.
I'm not using cross-origin requests, these are all same-origin. I have tried using withCredentials true with the same result but this property shouldn't be needed anyway as it's same-origin and Chrome and Firefox work without it.
There doesn't seem to be a pattern to which files fail to send the request cookie. Sometimes at random (but rarely), all requests load ok.
It's not a sequential failure, most requests succeed to begin with then a sequence of fails with some successes in between.
The Express server isn't destroying the sessions, they keep accumulating. Failed requests have session ids that are not authenticated sessions.
What would cause Safari to randomly not send the request cookie for some XMLHTTPRequests?
It turned out to be the crossOrigin property, I must have been doing cross-origin requests:
https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_settings_attributes
Most of my loaded files had their crossOrigin property set as "anonymous". For some reason, Chrome and Firefox seemed to ignore this and sent the cookie credentials anyway but Safari didn't. Setting this property to "use-credentials" worked fine:
img.crossOrigin = "use-credentials";
On my computer, can I login to my web app, grab the cookie values, and have my selenium script use it to test the web app without having to login?
Is it also possible to modify the cookie expiration after logging in on my computer so that the cookie will won't expire when the selenium tests are running?
Is it possible to avoid having to write additional selenium logic to use the login form? If not, does it raise a cookie expired exception so that I know I need to re-login?
What about testing cookieless session webapps like Meteor.js?
Using .NET I know you can set the cookies, or at least retrieve them. Try placing your cookies there.
driver.Manage().Cookies = Your_Cookie_Object_Here;