Remain logged into account using selenium - python

I'm trying to login to http://login.live.com, and stay logged in after closing the browser using pickle and cookies.
import pickle
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://login.live.com')
# i do my login here
pickle.dump(driver.get_cookies() , open("login_live.pkl","wb"))
browser.quit()
browser = webdriver.Chrome()
browser.get('https://google.com')
for cookie in pickle.load(open("login_live.pkl", "rb")):
driver.add_cookie(cookie)
browser.get('https://login.live.com')
The problem is that after directing to live.com, I don't remain logged into my account. I perform the same flow manually (obviously without loading cookies). Can't seem to figure out what is wrong, any help would be appreciated.

login.live.com is a redirection page and cookies are not associated with it. Use the page of cookies i.e. https://account.microsoft.com
So while re-loading the session, load the page and then load cookies -
import pickle
from selenium import webdriver
browser = webdriver.Chrome("./chromedriver")
browser.get('https://login.live.com')
pickle.dump(browser.get_cookies() , open("login_live.pkl","wb"))
browser.quit()
browser = webdriver.Chrome("./chromedriver")
browser.get('https://account.microsoft.com')
for cookie in pickle.load(open("login_live.pkl", "rb")):
browser.add_cookie(cookie)

Related

Selenium python to change the cookies of another site from one site

I want to change a cookie outside the site I'm visiting with python selenium like this (this is my chrome tab not selenium browser)
The site I entered: https://2captcha.com/tr/demo/hcaptcha?difficulty=difficult
Cookie site I use from frame:
newassets.hcaptcha.com
At first I tried to do something like:
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://newassets.hcaptcha.com')
cookie = {"name":"hc_accessibility","value":""}
driver.add_cookie(cookie)
driver.refresh()
time.sleep(2)
driver.get("https://2captcha.com/tr/demo/hcaptcha?difficulty=difficult")
time.sleep(2)
but when I entered the site, the hcaptcha_accecibilty cookies were not there (this is my selenium browser tab)

How to get cookies saved in browser with python and Selenium

I'm getting to use the cookie that is already in the browser and use it in Selenium, I know you can't use it using Selenium only, but is there any library I can use to save cookies in json in a variable and use it in Selenium? How can I extract the cookie saved in the browser with python? not only Chrome but others also preferably.
This is my code currently:
option = Options()
option.add_argument("--no-sandbox")
driver = webdriver.Chrome(options=option)
driver.get("https://google.com")
wait = WebDriverWait(driver, 5)
How can I get the cookie from the browser, save it in json format and use it with Selenium?
import pickle
import os
from selenium import webdriver
import time
option = webdriver.ChromeOptions()
option.add_argument("--no-sandbox")
driver = webdriver.Chrome(options=option)
driver.get("https://google.com")
time.sleep(5)
if os.path.exists('cookies.pkl'):
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
driver.add_cookie(cookie)
driver.refresh()
sleep(5)
pickle.dump(driver.get_cookies(), open("cookies.pkl", "wb"))
pickle will help you save and add cookies. But, be sure to add them to the correct domain otherwise you might get errors.

Consistent cookies with selenium

I'm trying to do some authorization with selenium on a page with login.
the page has captcha so it's not possible automatize the login process,
is there a way to login, and save the cookies and then import them to another browser instance?
In order to bypass the CAPTCHA when scraping, you have to manually solve a CAPTCHA and export the cookies you get. Now, every time you open a Selenium WebDriver, make sure you add the cookies you exported. save all cookies to be on the safe side.
If you want an additional layer of stability in your scrapes, you should export several cookies and have your script randomly select one of them each time you get to login page.
These cookies have a long expiration date so you wouldn't need to get new cookies every day.
For help on saving and loading cookies in Python and Selenium, you can use this code:
You can save the current cookies as a Python object using pickle. For example:
import pickle
import selenium.webdriver
driver = selenium.webdriver.Firefox()
driver.get("http://www.google.com")
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
And later to add them back:
import pickle
import selenium.webdriver
driver = selenium.webdriver.Firefox()
driver.get("http://www.google.com")
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
driver.add_cookie(cookie)

Webscrape LinkedIn Users

Goal is to scrape information from people/users
Here is my code - Trying to get into the url to eventually scrape data from the search.
However when executing the code, it prompts the log in page. This is where im currently stuck
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
productlinks=[]
test1=[]
options = Options()
options.headless = True
driver = webdriver.Chrome(ChromeDriverManager().install())
url = "https://www.linkedin.com/search/results/people/?currentCompany=%5B%221252860%22%5D&geoUrn=%5B%22103644278%22%5D&keywords=sales&origin=FACETED_SEARCH&page=2"
driver.get(url)
time.sleep(3)
username = driver.find_element_by_class_name('login-email')
username.send_keys('example123#gmail.com')
password = driver.find_element_by_class_name('login-password')
password.send_keys('Password123')
log_in_button = driver.find_element_by_class_name('login-submit')
log_in_button.click()
There are 3 methods
Add the login code logic in your code by clicking the login button and send the login credentials using send_keys()
Disable headless by removing options.headless = True and manually login yourself
Since LinkedIn uses cookies to validate session, so you can login once and store up the cookies somewhere else and inject back to your session every time you launch selenium driver.
For getting the cookies,
# Go to the correct domain
driver.get("https://www.example.com")
# get all the cookies from this domain
driver = browser.get_cookies()
# store it somewhere, maybe a text file
or do it manually and copy from Chrome Dev Tools
For restoring the cookies
# Go to the correct domain
driver.get("https://www.example.com")
# get back the cookies
cookies = {‘name’ : ‘foo’, ‘value’ : ‘bar’}
driver.add_cookies(cookies)
Reference: LinkedIn Cookies Policy

function driver.delete_all_cookies() in selenium python doesn't delete all cookies

I'm trying to delete all cookies from browser and then checking that no cookies remained. after that I ran my code I saw that still, some cookies remained... what's the problem? thanks
from selenium import webdriver
driver = webdriver.Chrome(executable_path=
"C:\\Chrome\\chromedriver.exe")
driver.get("http://www.walla.co.il")
driver.delete_all_cookies()
lst = driver.get_cookies()
for cookie in lst:
print(cookie)
I think this problem is more related to the load time from the website.
I just try your code adding a time.sleep(5) and the cookies are deleted correctly.
Probably when you are trying to delete the cookies the page hasn't finished loading

Categories

Resources