python selenium 等待页面加载_Python Selenium等待页面无条件加载

我希望硒在截图之前先等待。当我使用time.sleep(1)时,出现以下错误:

追溯(最近一次通话最近):driver.save_screenshot('page.png')中文件“ test.py”,第12行,文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ save_screenshot中的site-packages / selenium / webdriver / remote / we bdriver.py“行1033返回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“,第1010行,位于get_screenshot_as_file png = self.get_screenshot_as_png()文件” /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-在get_screenshot_as_png中的packages / selenium / webdriver / remote / we bdriver.py“行1042行,返回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中返回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,执行返回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 resp = 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()文件” /Library/Frameworks/Python.framework/Versions/3.6/ lib / python3.6 / http / client.py”,第258行,位于_read_status行= str(self.fp.readline(_MAXLINE + 1),“ iso-8859-1”)文件“ / Library / Frameworks / Python。 framework / Versions / 3.6 / lib / python3.6 / socket.py“,行586,读入以返回self._sock.recv_into(b)ConnectionResetError:[Errno 54]对等重置连接/read/lib/python3.6/socket.py"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py“中的行586,返回self._sock.recv_into(b)ConnectionResetError:[Errno 54]对等重置连接/read/lib/python3.6/socket.py"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py“中的行586,返回self._sock.recv_into(b)ConnectionResetError:[Errno 54]对等重置连接

这是我的代码:

fromseleniumimportwebdriverfromselenium.webdriver.firefox.optionsimportOptionsfromselenium.webdriver.support.uiimportWebDriverWaitimportosimporttime

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上没有更多可加载的内容,我只是举了这个示例来说明这个问题。)

解决方案

使用and循环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 loadloading_finished=0# Set flag to 0whileloading_finished==0:# Repeat while flag = 0try:sleep(random.uniform(0.1,0.5))# wait some timewebsite=driver.get(URL)# try to load for n secondsloading_finished=1# Set flag to 1 and exit while loopexcept:logger.warn("timeout - retry")# Indicate load failelse:driver.save_screenshot('page.png')# In case of flag = 1driver.close()driver.quit()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值