Can't fetch the profile name using Selenium after logging in using requests

It's probably more appropriate to use the Stack Exchange API rather than scrape the site, but in any case..

There are a few problems:

  1. You will sometimes get a captcha challenge.

  2. Leaving the default requests headers increases the odds of getting a captcha, so override it with one from a traditional browser.

  3. You need to use requests.Session() to maintain the cookies from both of the first two requests.

  4. Before adding the cookies from the requests session, you need to make an initial request with webdriver and clear any created cookies.

Taking those things into account, I was able to get it to work with the following:

import requests
from bs4 import BeautifulSoup
from selenium import webdriver

url = "https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3a%2f%2fstackoverflow.com%2f"

headers = {
    "User-Agent": (
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
        "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36"
    )
}

s = requests.Session()

req = s.get(url, headers=headers)
payload = {
    "fkey": BeautifulSoup(req.text, "lxml").select_one("[name='fkey']")["value"],
    "email": "YOUR_EMAIL",
    "password": "YOUR_PASSWORD",
}

res = s.post(url, headers=headers, data=payload)

if "captcha" in res.url:
    raise ValueError("Encountered captcha")

driver = webdriver.Chrome()

try:
    driver.get(res.url)
    driver.delete_all_cookies()

    for cookie in s.cookies.items():
        driver.add_cookie({"name": cookie[0], "value": cookie[1]})

    driver.get(res.url)

    item = driver.find_element_by_css_selector("div[class^='gravatar-wrapper-']")
    print(item.get_attribute("title"))
finally:
    driver.quit()

You need to be on the domain that the cookie will be valid for.

Before calling driver.add_cookie(), you must first navigate to [any] page from that domain... so, make an additional call to driver.get(url) before attempting to add cookies. Even an error page will suffice:

driver.get('https://stackoverflow.com/404')

for example...

change this in your code:

driver.add_cookie(cookie_item[0])
driver.get(res.url)

to this:

driver.get('https://stackoverflow.com/404')
driver.add_cookie(cookie_item[0])
driver.get(res.url)