Python selenium等待页面加载而没有条件

时间:2018-06-17 20:05:56

标签: python selenium selenium-webdriver

我想让selenium在拍摄截图之前等待。当我使用time.sleep(1)时,它给了我这个错误:

追踪(最近一次通话):   文件“test.py”,第12行,in     driver.save_screenshot( 'page.png')   文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/we bdriver.py“,第1033行,在save_screenshot中     return self.get_screenshot_as_file(filename)   文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/we bdriver.py“,第1010行,在get_screenshot_as_file中     png = self.get_screenshot_as_png()   文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/we bdriver.py“,第1042行,在get_screenshot_as_png中     return base64.b64decode(self.get_screenshot_as_base64()。encode('ascii'))   文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/we bdriver.py“,第1052行,在get_screenshot_as_base64中     return self.execute(Command.SCREENSHOT)['value']

文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/we bdriver.py“,第312行,执行中     response = self.command_executor.execute(driver_command,params)   文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/re mote_connection.py“,第472行,执行中     return self._request(command_info [0],url,body = data)   文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/re 在_request中,mote_connection.py“,第496行     resp = self._conn.getresponse()   在getresponse中输入文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py”,第1331行     response.begin()   文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py”,第297行,开头     版本,状态,原因= self._read_status()   在_read_status中输入文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py”,第258行     line = str(self.fp.readline(_MAXLINE + 1),“iso-8859-1”)   文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py”,第586行,在readinto中     return self._sock.recv_into(b) ConnectionResetError:[Errno 54]通过对等方重置连接

这是我的代码:

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()

如何在不收到此错误的情况下等待页面加载?

我知道你可以等待一个元素加载我只是想等待没有任何条件。

(当然,在google.com上没有更多内容可以加载我只是用这个例子来制作复活节的问题。)

2 个答案:

答案 0 :(得分:1)

将无条件等待添加到硒driver.get(URL)driver.set_page_load_timeout(n) n = time/seconds并循环:

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()

答案 1 :(得分:0)

# 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

此外:

# 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