I am currently using selenium in python to do something that requires an infinite end loop to observe what I want, here is a code snippet:
records = set() fileHandle = open('d:/seizeFloorRec.txt', 'a') fileHandle.write('\ncur time: '+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+'\n') driver = webdriver.Chrome() while(True): try: print "time: ", time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) subUrls = aMethod(driver) # a irrelevant function which returns a list time.sleep(2) for i in range(0, len(subUrls)): print "cur_idx=["+str(i)+"], max_cnt=["+str(len(subUrls))+"]" try: rtn = monitorFloorKeyword(subUrls[i]) time.sleep(1.5) if(rtn[0] == True): if(rtn[1] not in records): print "hit!" records.add(rtn[1]) fileHandle.write(rtn[1]+'\t'+rtn[2].encode('utf-8')+'\n') fileHandle.flush() else: print "hit but not write." except Exception as e: print "exception when get page: ", subUrls[i] print e.__doc__ continue print "sleep 5*60 sec..." time.sleep(300) # PROBLEM LIES HERE!!! print "sleep completes." except Exception as e: print 'exception!' print e.__doc__ time.sleep(20)
it always sets unpredictably in time.sleep(300) , and the output is "sleep 5 * 60 sec ..." yet without "ending the sleep."
Can someone give me some probable cause of this phenomenon? Many thanks!
UPDATED
I found a similar problem here , but I really don't understand what he wants to say. Hope this will contribute to my problem.
LAST TEST
Using chromedriver I added driver.get("about:blank") right before each return line in each function, as shown below, to force the page to stop asynchronously loading on the current page. and this forced stop operation causes ERROR ipc_channel_win.cc (370)] channel error: 109 , which does NOT affect the operation of my program. time.sleep this affect my time.sleep function?
def retrieveCurHomePageAllSubjectUrls(driver): uri = "http://www.example.com/main.php?page=1" driver.get(uri) element = driver.find_elements_by_class_name('subject') subUrls = [] for i in range(0, len(element)): subUrls.append(element[i].get_attribute('href').encode('utf-8')) driver.get("about:blank") #This is what I add return subUrls def monitorFloorKeyword(subUrl): driver.get(subUrl) title = driver.find_element_by_id('subject_tpc').text content = driver.find_element_by_id('read_tpc').text if(title.find(u'keyword') >= 0 or content.find(u'keyword') >= 0): driver.get("about:blank") #This is what I add return (True,subUrl,title,content) driver.get("about:blank") #This is what I add return (False,)
SEEM TO THE END
As I said above, there is a channel error right after I have driver.get("about:blank") , however, the good news is that this time everything works fine. If someone knows something about selenium that is relevant to this post, please let me know if I would.