How to use save_cookies method in SeleniumBase

Best Python code snippet using SeleniumBase

classes.py

Source: classes.py Github

copy

Full Screen

...68 :return: The file.69 """70 return self._cookies_file71 @property72 def save_cookies(self):73 """74 Property to retrieve information if it is to save cookies.75 :return: Bool.76 """77 return self._save_cookies78class ChromeBrowser(BaseBrowser):79 """80 Chrome browser class.81 """82 def __init__(self, token=None, session=None, save_cookies=False, no_headless=False):83 super().__init__(token=token, session=session, save_cookies=save_cookies, no_headless=no_headless)84 self._options = ChromeOptions()85 if session:86 self._options.add_argument("--user-data-dir={}".format(session))...

Full Screen

Full Screen

task1_2.py

Source: task1_2.py Github

copy

Full Screen

2from selenium import webdriver3import time4import pickle5import json6def save_cookies(driver, location):7 pickle.dump(driver.get_cookies(), open(location, "wb"))8driver = webdriver.Chrome("C:\\Program Files (x86)\\chromedriver.exe")9driver.get("https:/​/​www.ebay.com")10save_cookies(driver, "C:\\Users\\leven\\Desktop\\Python\\cookies2.txt") # save cookies11time.sleep(1)12# MACBOOK PRO13driver.find_element_by_name("_nkw").send_keys("macbook pro") # write in search bar14driver.find_element_by_id("gh-btn").click() # click search15time.sleep(1)16driver.find_element_by_xpath("/​/​*[@id='srp-river-results']/​ul/​li[1]/​div/​div[2]/​a/​h3").click() # click on 1st element17save_cookies(driver, "C:\\Users\\leven\\Desktop\\Python\\cookies2.txt") # save cookies18print(driver.get_cookies())19driver.save_screenshot("macbook_pro1.png") # screenshot20driver.back() # back21time.sleep(1)22driver.find_element_by_xpath("/​/​*[@id='srp-river-results']/​ul/​li[2]/​div/​div[2]/​a/​h3/​span").click() # click on 3rd element23save_cookies(driver, "C:\\Users\\leven\\Desktop\\Python\\cookies2.txt") # save cookies24print(driver.get_cookies())25driver.save_screenshot("macbook_pro2.png")26driver.back()27time.sleep(1)28driver.find_element_by_xpath("/​/​*[@id='srp-river-results']/​ul/​li[3]/​div/​div[2]/​a/​h3").click() # click on 2nd element29save_cookies(driver, "C:\\Users\\leven\\Desktop\\Python\\cookies2.txt") # save cookies30driver.save_screenshot("macbook_pro3.png")31driver.back()32driver.back() # go back to search bar33# DELL XPS34driver.find_element_by_name("_nkw").send_keys("dell xps") 35driver.find_element_by_id("gh-btn").click()36driver.find_element_by_xpath("/​/​*[@id='srp-river-results']/​ul/​li[1]/​div/​div[2]/​a/​h3").click() 37save_cookies(driver, "C:\\Users\\leven\\Desktop\\Python\\cookies2.txt") # save cookies38driver.save_screenshot("dell_xps1.png")39driver.back()40driver.find_element_by_xpath("/​/​*[@id='srp-river-results']/​ul/​li[2]/​div/​div[2]/​a/​h3").click()41save_cookies(driver, "C:\\Users\\leven\\Desktop\\Python\\cookies2.txt") # save cookies42driver.save_screenshot("dell_xps2.png")43driver.back()44driver.find_element_by_xpath("/​/​*[@id='srp-river-results']/​ul/​li[3]/​div/​div[2]/​a/​h3").click()45save_cookies(driver, "C:\\Users\\leven\\Desktop\\Python\\cookies2.txt") # save cookies46driver.save_screenshot("dell_xps3.png")47driver.back()48driver.back() 49# NIKE SHOES50driver.find_element_by_name("_nkw").send_keys("nike shoes")51driver.find_element_by_id("gh-btn").click()52driver.find_element_by_xpath("/​/​*[@id='srp-river-results']/​ul/​li[1]/​div/​div[2]/​a/​h3").click() 53save_cookies(driver, "C:\\Users\\leven\\Desktop\\Python\\cookies2.txt") # save cookies54driver.save_screenshot("nike_shoes1.png")55driver.back()56driver.find_element_by_xpath("/​/​*[@id='srp-river-results']/​ul/​li[2]/​div/​div[2]/​a/​h3").click()57save_cookies(driver, "C:\\Users\\leven\\Desktop\\Python\\cookies2.txt") # save cookies 58driver.save_screenshot("nike_shoes2.png")59driver.back()60driver.find_element_by_xpath("/​/​*[@id='srp-river-results']/​ul/​li[3]/​div/​div[2]/​a/​h3").click() 61save_cookies(driver, "C:\\Users\\leven\\Desktop\\Python\\cookies2.txt") # save cookies62driver.save_screenshot("nike_shoes3.png")63driver.back()64driver.back()65# MELLER GLASSES66driver.find_element_by_name("_nkw").send_keys("meller glasses")67driver.find_element_by_id("gh-btn").click()68time.sleep(1)69driver.find_element_by_xpath("/​/​*[@id='srp-river-results']/​ul/​li[1]/​div/​div[2]/​a").click()70save_cookies(driver, "C:\\Users\\leven\\Desktop\\Python\\cookies2.txt") # save cookies71driver.save_screenshot("meller_glasses1.png")72driver.back()73driver.find_element_by_xpath("/​/​*[@id='srp-river-results']/​ul/​li[2]/​div/​div[2]/​a/​h3").click() 74save_cookies(driver, "C:\\Users\\leven\\Desktop\\Python\\cookies2.txt") # save cookies75driver.save_screenshot("meller_glasses2.png")76driver.back()77driver.find_element_by_xpath("/​/​*[@id='srp-river-results']/​ul/​li[3]/​div/​div[2]/​a/​h3").click() 78save_cookies(driver, "C:\\Users\\leven\\Desktop\\Python\\cookies2.txt") # save cookies79driver.save_screenshot("meller_glasses3.png")80driver.back()...

Full Screen

Full Screen

save_cookies.py

Source:save_cookies.py Github

copy

Full Screen

...8from time import sleep9from selenium import webdriver10from h20_learn.web_auto_test.config.get_config import dir_path11class SaveCookies:12 def save_cookies(self):13 self.driver = webdriver.Chrome()14 self.driver.implicitly_wait(5)15 self.driver.get('https:/​/​work.weixin.qq.com/​wework_admin/​frame#index')16 sleep(20)17 cookies = self.driver.get_cookies()18 print(cookies)19 cookies_file = dir_path /​ 'config' /​ 'cookies.json'20 with open(cookies_file,'w') as f:21 json.dump(cookies,f)22 self.driver.quit()23save_cookies = SaveCookies()24if __name__ == '__main__':...

Full Screen

Full Screen

Blogs

Check out the latest blogs from LambdaTest on this topic:

How Testers Can Remain Valuable in Agile Teams

Traditional software testers must step up if they want to remain relevant in the Agile environment. Agile will most probably continue to be the leading form of the software development process in the coming years.

Considering Agile Principles from a different angle

In addition to the four values, the Agile Manifesto contains twelve principles that are used as guides for all methodologies included under the Agile movement, such as XP, Scrum, and Kanban.

How To Refresh Page Using Selenium C# [Complete Tutorial]

When working on web automation with Selenium, I encountered scenarios where I needed to refresh pages from time to time. When does this happen? One scenario is that I needed to refresh the page to check that the data I expected to see was still available even after refreshing. Another possibility is to clear form data without going through each input individually.

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run SeleniumBase automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful