Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

python - Chrome stores cookies too late local in profile path

Since it's not possible via webdriver.get_cookies() to get all cookies (same domain and 3rd party cookies), I decided to read the Cookies file - which is stored in user profile path as SQLite.

But strangely I just saw that cookies are not being saved to this file in real time. I'm also not sure when the cookies are being stored. I have to wait about 10 seconds then the I can find all cookies there. Is there any idea how to read all cookies in real time for selenium web driver?

question from:https://stackoverflow.com/questions/65884239/chrome-stores-cookies-too-late-local-in-profile-path

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ideally before collecting and printing the cookies you need to induce WebDriverWait for the visibility_of_element_located() for a visible element and then collect the as follows:

webdriver.get(url)
WebDriverWait(webdriver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "element_css")))
print(webdriver.get_cookies())

Or induce WebDriverWait for the element_to_be_clickable() for an interactive element and then collect the as follows:

webdriver.get(url)
WebDriverWait(webdriver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "element_css")))
print(webdriver.get_cookies())

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...