update_apollo_cookie.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support.ui import WebDriverWait
  4. from selenium.webdriver.chrome.options import Options
  5. username = "apollo"
  6. password = "admin"
  7. chrome_options = Options()
  8. chrome_options.add_argument('--headless')
  9. driver = webdriver.Chrome("chromedriver", options=chrome_options)
  10. login_url = 'http://192.168.31.230:4379/signin'
  11. driver.get(login_url)
  12. # find username/email field and send the username itself to the input field
  13. login_form = driver.find_element(by=By.ID, value='login-form')
  14. login_form.find_element(by=By.NAME, value='username').send_keys(username)
  15. login_form.find_element(by=By.NAME, value='password').send_keys(password)
  16. login_form.find_element(by=By.ID, value='login-submit').click()
  17. WebDriverWait(driver=driver, timeout=10).until(
  18. lambda x: x.execute_script("return document.readyState === 'complete'")
  19. )
  20. cookies = driver.get_cookies()
  21. cookie_kv_str_list = ['{}={}'.format(c['name'], c['value']) for c in cookies]
  22. cookie_str = ';'.join(cookie_kv_str_list)
  23. print('cookie str:{}'.format(cookie_str))
  24. cookie_save_path = '../config/cookies'
  25. with open(cookie_save_path, mode='w') as cookie_file:
  26. cookie_file.write(cookie_str)
  27. print('done')