Asked  7 Months ago    Answers:  5   Viewed   51 times

How can I save all cookies in Python's Selenium WebDriver to a .txt file, and then load them later?

The documentation doesn't say much of anything about the getCookies function.

 Answers

94

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)
Tuesday, June 1, 2021
 
joostvandriel
answered 7 Months ago
11

This is a known, currently open, issue in selenium 2.49. As a workaround, downgrade to 2.48:

pip install selenium==2.48

I would also improve the locator to:

//div[@class="pagebook"]/div[. = "2"]
Thursday, August 5, 2021
 
buymypies
answered 4 Months ago
40

You have to decide on a location to store the cached profile, then use functions in the os library to check if there is a file in that location, and load it. To cache the profile in the first place, you should be able to get the path to the profile from webdriver.firefox_profile.path, then copy the contents to your cache location.

All that said, I'd really recommend against this. By caching the profile created at test runtime, you are making your test mutate based upon previous behavior, which means it is no longer isolated and reliably repeatable. I'd recommend that you create a profile separately from the test, then use that as the base profile all the time. This makes your tests predictably repeatable. Selenium is even set up to work well with this pattern, as it doesn't actually use the profile you provide it, but instead duplicates it and uses the duplicate to launch the browser.

Saturday, August 7, 2021
 
answered 4 Months ago
33

It's easier and saves a few temporary arrays to just reinterpret the array as a real array.

Saving:

numpy.savetxt('outfile.txt', array.view(float))

Loading:

array = numpy.loadtxt('outfile.txt').view(complex)

If you prefer to have real and imaginary part on the same line in the file, you can use

numpy.savetxt('outfile.txt', array.view(float).reshape(-1, 2))

or

array = numpy.loadtxt('outfile.txt').view(complex).reshape(-1)

respectively.

(Note that neither view() nor reshape() copies the array -- it will just reinterpret the same data in a different way.)

Addendum from the question asker:

If you want to save more than one complex array in the same file, you can do it like so:

numpy.savetxt('outfile.txt', numpy.column_stack([
    array1.view(float).reshape(-1, 2),
    array2.view(float).reshape(-1, 2),
]))

array1, array2 = numpy.loadtxt('outfile.txt', unpack=True).view(complex)

The reshaping is necessary because numpy.view() doesn't operate on strided arrays.

Saturday, September 11, 2021
 
linjuming
answered 3 Months ago
31

I would just use all():

def is_data_objet_deleted(self, name):
    table = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_data_objects_ct_fields_body')))
    rows = table_id.find_elements(By.TAG_NAME, "tr")

    result = all(row.find_elements(By.TAG_NAME, "td")[2].text != name
                 for row in rows)

    # save a screenshot if there was name found
    if not result:
        self.save_screenshot("data_objects_page_saved_details")
    return result

Basically, this would return True if all the names don't equal to name which is in other words: return True if name is not there.

As a side note, you are handling NoSuchElementException, but it would never be thrown by any method used inside the try block - find_elements() would return an empty list if no elements matching a locator found.

Saturday, October 23, 2021
 
clean_coding
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share