I am trying to save the cookies for my selenium automation script in order to avoid login process for future automation.
This is the original script which works fine
class SeleniumDriver(object):
def __init__(
self,
# chromedriver path
driver_path='chromedriver.exe',
# pickle file path to store cookies
cookies_file_path = 'cookies.pkl',
# list of websites to reuse cookies with
cookies_websites=["https://website.com"] # I would like to replace this website by an argument
):
self.driver_path = driver_path
self.cookies_file_path = cookies_file_path
self.cookies_websites = cookies_websites
#PREVIOUS VERSION WITH NORMAL CHROME chrome_options = webdriver.ChromeOptions()
chrome_options = uc.ChromeOptions()
#PREVIOUS VERSION WITH NORMAL CHROME self.driver = webdriver.Chrome(
self.driver = uc.Chrome(
executable_path=self.driver_path,
options=chrome_options
)
try:
# load cookies for given websites
cookies = pickle.load(open(self.cookies_file_path, "rb"))
for website in self.cookies_websites:
self.driver.get(website)
for cookie in cookies:
self.driver.add_cookie(cookie)
self.driver.refresh()
except Exception as e:
# it'll fail for the first time, when cookie file is not present
logger.error(str(e))
logger.error("ERROR : Error loading cookies")
def save_cookies(self):
# save cookies
cookies = self.driver.get_cookies()
pickle.dump(cookies, open(self.cookies_file_path, "wb"))
def close_all(self):
# close all open tabs
if len(self.driver.window_handles) < 1:
return
for window_handle in self.driver.window_handles[:]:
self.driver.switch_to.window(window_handle)
self.driver.close()
def quit(self):
self.save_cookies()
self.close_all()
self.driver.quit()
selenium_object = SeleniumDriver()
driver = selenium_object.driver
You can see in the code during the creation of object, the url of website "https://website.com" is used to create the cookies for this website.
If tomorrow, I use my script for "https://anotherwebsite.com", I would need to pass hthe website as arguments like this for example:
class SeleniumDriver(object,website): # the variable website is passed as argument in class creation
....
website="https://anotherwebsite.com"
selenium_object = SeleniumDriver(website)
My problem is I alsways have this error output:
NameError: name 'website' is not defined
Here is full code which doesn't work:
class SeleniumDriver(object,website):
def __init__(
self,
# chromedriver path
driver_path='chromedriver.exe',
# pickle file path to store cookies
cookies_file_path = 'cookies.pkl',
# list of websites to reuse cookies with
cookies_websites=[website]
):
self.driver_path = driver_path
self.cookies_file_path = cookies_file_path
self.cookies_websites = cookies_websites
#PREVIOUS VERSION WITH NORMAL CHROME chrome_options = webdriver.ChromeOptions()
chrome_options = uc.ChromeOptions()
#PREVIOUS VERSION WITH NORMAL CHROME self.driver = webdriver.Chrome(
self.driver = uc.Chrome(
executable_path=self.driver_path,
options=chrome_options
)
try:
# load cookies for given websites
cookies = pickle.load(open(self.cookies_file_path, "rb"))
for website in self.cookies_websites:
self.driver.get(website)
for cookie in cookies:
self.driver.add_cookie(cookie)
self.driver.refresh()
except Exception as e:
# it'll fail for the first time, when cookie file is not present
logger.error(str(e))
logger.error("ERROR : Error loading cookies")
def save_cookies(self):
# save cookies
cookies = self.driver.get_cookies()
pickle.dump(cookies, open(self.cookies_file_path, "wb"))
def close_all(self):
# close all open tabs
if len(self.driver.window_handles) < 1:
return
for window_handle in self.driver.window_handles[:]:
self.driver.switch_to.window(window_handle)
self.driver.close()
def quit(self):
self.save_cookies()
self.close_all()
self.driver.quit()
website="https://mywebsite.com"
selenium_object = SeleniumDriver(website)
driver = selenium_object.driver
When I run this code, I get this error output:
class SeleniumDriver(object,website):
NameError: name 'website' is not defined