Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from selenium import webdriver
import requests
username = "Your Username"
password = "Your Password"
driver = webdriver.Chrome()
# authenticate using username, password
login_url = "https://your.target_website.com/login/"
driver.get(login_url)
driver.find_element_by_id("username").send_keys(username)
driver.find_element_by_id("password").send_keys(password + "\n")
# interact with target web elements to submit a form
driver.get(target_url)
driver.find_element_by_id("some_button").click()
# retreive download url
downlaod_url = driver.find_element_by_id("div_with_download_link").find_element_by_tag_name("a").get("href")
chunk_size = 4096
download_filename = "logo.png"
# your custom headers
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36",
"Connection": "keep-alive",
}
s = requests.Session()
# retrieve and set cookies from selenium to requests session
for selenium_cookie in driver.get_cookies():
cookie = requests.cookies.create_cookie(selenium_cookie['name'], selenium_cookie['value'])
s.cookies.set_cookie(cookie)
with s.get(download_url, stream=True, headers=headers) as r:
with open(download_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size):
if chunk:
f.write(chunk)
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.