1
votes

I want selenium to wait before taking a screenshot. When I use time.sleep(1) it gives me this error:

Traceback (most recent call last): File "test.py", line 12, in driver.save_screenshot('page.png') File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/we bdriver.py", line 1033, in save_screenshot return self.get_screenshot_as_file(filename) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/we bdriver.py", line 1010, in get_screenshot_as_file png = self.get_screenshot_as_png() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/we bdriver.py", line 1042, in get_screenshot_as_png return base64.b64decode(self.get_screenshot_as_base64().encode('ascii')) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/we bdriver.py", line 1052, in get_screenshot_as_base64 return self.execute(Command.SCREENSHOT)['value']

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/we bdriver.py", line 312, in execute response = self.command_executor.execute(driver_command, params) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/re mote_connection.py", line 472, in execute return self._request(command_info[0], url, body=data) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/re mote_connection.py", line 496, in _request resp = self._conn.getresponse() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1331, in getresponse response.begin() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 297, in begin version, status, reason = self._read_status() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 258, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 586, in readinto return self._sock.recv_into(b) ConnectionResetError: [Errno 54] Connection reset by peer

This is my code:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
import os
import time

options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=options, executable_path = 
'/usr/local/bin/geckodriver', log_path=os.devnull)
driver.get('https://google.com/')
time.sleep(5)
driver.save_screenshot('page.png')
driver.quit()

How do I wait for the page to load without getting this error?

I know you can wait for an element to load I just want to wait without any condition for it.

(of course, there is nothing more to load on google.com I just made this example to make the question easter.)

2
The error isn't due to the sleep, it's due to the save_screenshot() call.JeffC

2 Answers

1
votes

to add a unconditional wait to driver.get(URL) in selenium, driver.set_page_load_timeout(n) with n = time/seconds and loop:

driver.set_page_load_timeout(n)        # Set timeout of n seconds for page load
loading_finished = 0                   # Set flag to 0
while loading_finished == 0:           # Repeat while flag = 0
    try:
       sleep(random.uniform(0.1, 0.5)) # wait some time
       website = driver.get(URL)       # try to load for n seconds
       loading_finished = 1            # Set flag to 1 and exit while loop
    except:
       logger.warn("timeout - retry")  # Indicate load fail
else:
    driver.save_screenshot('page.png') # In case of flag = 1
    driver.close()
    driver.quit()
0
votes
# this code will will keep looping until the element is find so whatever how 
# much time the page will take time to load .    
driver = webdriver.Firefox(executable_path=r'./geckodriver')
itm = ""
while(True) : 
     try : 
         itm = driver.find_element_by_xpath("your xpath elem)
         break
     except : 
      pass

Furthermore:

# this function will find all the ements with xpath EXPR
def Finds(EXPR) :
    itms = ""
    while(True) : 
        try : 
            itms = driver.find_elements_by_xpath(EXPR)
            break
        except : 
            pass
    return itms
#this function will find the elem had xpath EXPR
def Find(EXPR) :
    itm = ""
    while(True) : 
        try : 
            itm = driver.find_element_by_xpath(EXPR)
            break
        except : 
            pass
    return itm
#this function will find textbox or any thing you can insert into a text , this elem had xpath F , it insert text I
def Find_Insert(F,I) : 
    it = ""
    x1 = ""
    while(True) : 
        try : 
            print "find " , F
            x1 = driver.find_element_by_xpath(F)
            print("1")
            break
        except : 
            pass
    it = x1 
    while(True) : 
        try :
            print "sending " , I
            it.send_keys(I)
            break
        except : 
            pass
#this function will find textbox or any thing you can insert into a text , this elem had xpath F , it insert text I and then press key C
def Find_Insert_Click(F,I,C) : 
    it = ""
    x1 = ""
    while(True) : 
        try : 
            print "find " , F
            x1 = driver.find_element_by_xpath(F)
            print("1")
            break
        except : 
            pass
    it = x1 
    while(True) : 
        try :
            print "sending " , I
            it.send_keys(I)
            break
        except : 
            pass
    x1 = it  
    while(True) : 
        try : 
            print "Submit " , F
            it.send_keys(C)
            break
        except : 
            pass  
# this function will the elleent with xpath F and then clicke it 
def Find_Click(F) :
    it = ""
    x1 = ""
    while(True) : 
        try : 
            print "find " , F
            x1 = driver.find_element_by_xpath(F)
            break
        except : 
            pass
    it = x1 
    while(True) : 
        try : 
            print "click" , F
            it.click()
            break
        except : 
            pass