在使用webdriver的get方法打开页面时,可能页面须要加载的元素较多致使加载时间很长,而时间过长会致使后面的操做没法进行,甚至直接报错;因此在页面加载到必定时间,实际须要定位的元素很大可能上已经加载出来时,须要中止页面的加载,进而进行下面的操做。web
下面是源码:浏览器
def set_page_load_timeout(self, time_to_wait): """ Set the amount of time to wait for a page load to complete before throwing an error. :Args: - time_to_wait: The amount of time to wait :Usage: driver.set_page_load_timeout(30) """ try: self.execute(Command.SET_TIMEOUTS, { 'pageLoad': int(float(time_to_wait) * 1000)}) except WebDriverException: self.execute(Command.SET_TIMEOUTS, { 'ms': float(time_to_wait) * 1000, 'type': 'page load'})
能够看到set_page_load_timeout()设置网页超时加载时间须要传入一个time_to_wait。网络
在Web自动化脚本执行时,浏览器打开网址会一直等待整个页面加载完成,但有时候页面想要的元素早就加载完成了,可是由于个别js之类的东西特别慢,只能干等着。url
下面是我公司开发一个项目时使用set_page_load_timeout()方法的具体例子。spa
# 请求URL @stop_loading def get_url(self,url,timeout=100): self.c_print('[%s] getting url...'%self.c_time()) self.driver.set_page_load_timeout(timeout) self.driver.get(url)
你们虽然不能彻底看懂,可是大概能够看出来这是一个经过get方法打开指定url的方法。code
有的时候网络可能会很卡,这就致使页面加载会很慢,甚至会直接报错。可是咱们的项目是确定要避免这种状况的,怎么避免呢?那就要使用set_page_load_timeout()啦。在使用get方法前使用此方法,就可使得网络不稳定、网速慢的问题不会直接致使任务失败。另外这个方法还能够组合其余方法,造成刷新、中止加载等操做。blog