原理:首先手動完成登入,把cookie儲存到本地,再吧cookies注入到瀏覽器裡面,就實現了自動登入,然後再透過click()實現簽到
1. Cookie儲存到本地
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import json
options = webdriver.ChromeOptions()
options.add_experimental_option('detach',True)
wd = webdriver.Chrome(service=Service(r'C:\\tools\\chromedriver.exe'),options=options)
wd.get('https://hifini.com/')
input('手動登入後,隨便輸入後繼續')
dictCookies = wd.get_cookies()
jsonCookies = json.dumps(dictCookies)
with open('cookies.txt','w') as f:
f.write(jsonCookies)
print('儲存成功')
2. 注入cookie
def hifi():
wd.get('https://hifini.com/')
with open('D:\code\Selenium\cookies.txt','r',encoding='utf-8') as f:
listCookies = json.loads(f.read())
wd.add_cookie(listCookies[1])
wd.get('https://hifini.com/')
sleep(3)
elem = wd.find_element(By.ID,'sign').click()
wd.implicitly_wait(10)
print('hifini.com簽到完成')