Using Selenium automation is cool, but struggling at login screen every run is not cool at all. We will login once and use that cookie until the session expired.
Let’s go easy in Python. (can applied yo your fav language)
from selenium import webdriver
from csv import DictReader
driver = webdriver.Chrome()
driver.get('http://www.facebook.com')
def getCookies(csvfile):
with open(csvfile, encoding='utf-8-sig') as file:
dict_reader = DictReader(file)
listdict = list(dict_reader)
return listdict
cookies = getCookies('cookie.csv')
for i in cookies:
driver.add_cookie()
driver.refresh()
For Chrome user, go to Application > Storage > Cookies
For Safari user, go to Storage > Cookies
then, copy all (ctrl+a) and paste into Excel, we will use column ‘Name’, ‘Value’, and ‘Domain’,
the rest just delete it,
save the file as .csv,
and to make sure the reader encoding correctly we put encoding='utf-8-sig'
in the code
Read the cookie file from the same directory, I saved as cookie.csv
Boom! now you can automate test any login-needed websites.
Just beware that some web has cookie expiration time, updating cookie.csv
is just fine. And some website might detect you as a bot, deal with it.
Ready, Set, Python!