【问题标题】:Python selenium wait for page to load without a conditionPython selenium 无条件等待页面加载
【发布时间】:2018-11-26 17:51:59
【问题描述】:

我希望 selenium 在截屏之前等待。当我使用 time.sleep(1) 时,它给了我这个错误:

Traceback(最近一次调用最后一次): 文件“test.py”,第 12 行,在 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 中 返回 self.get_screenshot_as_file(文件名) 文件“/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 返回 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 行,在执行中 响应 = 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 mote_connection.py”,第 496 行,在 _request 中 响应 = self._conn.getresponse() 文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py”,第 1331 行,在 getresponse 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 返回 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 上没有什么要加载的了

【问题讨论】:

  • 错误不是由于睡眠,而是由于save_screenshot()调用。

标签: python selenium selenium-webdriver


【解决方案1】:

在 selenium 中为 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()

【讨论】:

    【解决方案2】:
    # 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 
    

    【讨论】:

    • 嗨,欢迎来到 Stack Overflow。在回答已经有公认答案的问题时,请务必添加一些额外的见解,说明为什么您提供的回复是实质性的,而不是简单地回应原始发帖人已经审查过的内容。这在您提供的“纯代码”答案中尤其重要。
    猜你喜欢
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    相关资源
    最近更新 更多