pyhon學習之selenium模擬等待,直到....才,等待載入。
阿新 • • 發佈:2018-12-08
driver.implicitly_wait(30) driver.find_element_by_id("su").click() # Timeouts def implicitly_wait(self, time_to_wait): """ Sets a sticky timeout to implicitly wait for an element to be found, or a command to complete. This method only needs to be called one time per session. To set the timeout for calls to execute_async_script, see set_script_timeout. :Args: - time_to_wait: Amount of time to wait (in seconds) :Usage: driver.implicitly_wait(30) 設定查詢元素的非同步等待時長。 或者是一個即將完成的命令。 這個方法在一個session中只能呼叫一次,一個session就是一個會話,直到關閉瀏覽器視窗(session生存週期) 設定一步執行呼叫js,可以檢視set_script_timeout. """ if self.w3c: self.execute(Command.SET_TIMEOUTS, { 'implicit': int(float(time_to_wait) * 1000)}) else: self.execute(Command.IMPLICIT_WAIT, { 'ms': float(time_to_wait) * 1000})
我們可以在一些網頁中載入某一些網址的時候進行設定。就不需要通過sleep固定等待了。同時也推薦這種用法。