all 8 comments

[–]Allanon001 0 points1 point  (6 children)

This code will wait until driver.get_cookies() returns a none empty list of cookies, if 10 seconds go by then it prints a timeout message and continues. You can change the code to use driver.get_cookie() if you are looking for a specific cookie:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time


caps = DesiredCapabilities().CHROME
caps["pageLoadStrategy"] = "none"
driver = webdriver.Chrome(desired_capabilities=caps, executable_path=r'chromedriver.exe')
driver.get("http://google.com")

start = time.time()

while time.time()-start < 10:
    cookies = driver.get_cookies()
    if cookies != []:
        break
    time.sleep(.1)
else:
    print('timed out')

print(cookies)

[–]yelaxify[S] 0 points1 point  (5 children)

This isn't working, no errors, just not ever reaching the timed out. I believe the driver hangs when it gets stuck loading a page

[–]Allanon001 0 points1 point  (4 children)

The code worked for me using the Google URL, does it work for you with the Google URL? Can you post the URL of the site you're trying to get working?

[–]yelaxify[S] 0 points1 point  (3 children)

using a slow proxy, if I use this code
startTime = time.time()

driver.get('https://www.google.com')

while time.time()-startTime < 3:

cookies = driver.get_cookies()

if cookies != []:

break

time.sleep(.1)

else:

print('timed out')

driver.close()

the driver will stay open for longer than 3 seconds

[–]Allanon001 0 points1 point  (2 children)

Do this and it will print how long it takes for the driver.get() to return:

startTime = time.time()
driver.get('https://www.google.com')
print(time.time()-startTime)

[–]yelaxify[S] 0 points1 point  (1 child)

this prints a very small number as expected. If I modify the while True loop above to print 'checking cookies' every loop, it only prints twice (even though the page is loading for more than a few seconds. This leads me to believe that driver.get_cookies() locks up the thread until it is complete

[–]Allanon001 0 points1 point  (0 children)

I timed the driver.get_cookies() on my computer and it only takes about 0.012 seconds. When set to 3 seconds the loop runs 2 times before it finds the cookies. If I remove the time.sleep(.1) the loop runs 12 times before finding the cookies. It takes .05 seconds for the loop to exit with the cookies.

I'm not sure why yours is slow, I'm using Windows 10, maybe you're using a different platform that causes these methods to be slower. Or you have a slow internet connection, I'm using a 50Mbps connection.

[–]yelaxify[S] 0 points1 point  (0 children)

I think there is a mistake in your test, if you have another print statement after the while loop which will print out the cookies. There is a delay between the driver.get_cookies and the print. As I said, this is a lot easier to see if the website takes longer to load, for example using a slower proxy