Home > Blockchain >  Getting AttributeError for trying to access expected cookie
Getting AttributeError for trying to access expected cookie

Time:12-02

As shown bellow, the dictionary does contain a cookie with the attribute "name". For some reason the code worked once and is now throwing this error every time it is run :

File "/Users/me/Documents/Programming/browzine/last draft.py", line 201, in <lambda>
    cookie_list = list(map(lambda h: h.name '=' h.value '; ', driver.get_cookies()))
AttributeError: 'dict' object has no attribute 'name'

Here is what I get when I print the cookies with Selenium's driver.get_cookies() :

{'domain': '.idm.oclc.org', 'httpOnly': True, 'name': 'ezproxyl', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'snKQGpdG4GDPhMd'}
{'domain': '.idm.oclc.org', 'httpOnly': True, 'name': 'ezproxyn', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': 'snKQGpdG4GDPhMd'}
{'domain': '.idm.oclc.org', 'httpOnly': False, 'name': 'ezproxy', 'path': '/', 'secure': False, 'value': 'snKQGpdG4GDPhMd'}

My code:

def check_need_to_sign_in():
    new_tab = driver.window_handles
    driver.switch_to.window(str(new_tab[-1]))
    url = driver.current_url
    i = 0
    try:
        WebDriverWait(driver, 5).until(EC.element_to_be_clickable(
            (By.XPATH, "//input[@class='form-control ltr_override input ext-input text-box ext-text-box']")))
        print("Sign-in necessary")
      
        for cookie in driver.get_cookies():
            print(cookie)
        cookie_list = list(map(lambda h: h.name '=' h.value '; ', driver.get_cookies()))
        cookie_string = ''.join(cookie_list)
        print(cookie_string)
        headers = {}
        headers["Cookie"] = cookie_string
        s = req.session()
        s.headers.update(headers)
        for cookie in driver.get_cookies():
            print(cookie)
        sign_in()
        response = req.get(url, verify=False)
        while i < 1:
            print(response.ok)
            if response.ok == True:
                with open(f"{article_title[13:]}.pdf", 'wb') as f:
                    f.write(response.content)
                i =   1

CodePudding user response:

Use d['name'] or d.get('name') to get the value from the dictionary. d.name doesn't work in python.

  • Related