Python Selenium 等待页面加载没有条件



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

回溯(最近一次调用(: 文件"test.py",第 12 行,在 driver.save_screenshot("页面.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 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 行,执行中 响应 = self.command_executor.execute(driver_command, 参数( 文件 "/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 line = str(self.fp.readline(_MAXLINE + 1(, "iso-8859-1"( 文件"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py",第 586 行,读入 return self._sock.recv_into(b( 连接重置错误: [错误 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 我只是做了这个例子来使问题复活节。

要添加无条件等待以在硒中driver.get(URL),请使用n = time/seconds和循环driver.set_page_load_timeout(n)

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

最新更新