Selenium form submit part 2
source code
import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC browser = webdriver.Firefox() start_time = time.time() browser.get('http://xxx.xxx.xxx') old_page=browser.find_element_by_tag_name('html') print("submit: "+str(time.time()-start_time)) browser.find_element_by_id("save").click() print("wait: "+str(time.time()-start_time)) WebDriverWait(browser, 60).until(lambda browser: browser.execute_script('return document.readyState')=='complete') print("readyState: "+str(time.time()-start_time)) WebDriverWait(browser, 60).until(EC.staleness_of(old_page)) print("staleness: "+str(time.time()-start_time)) WebDriverWait(browser, 60).until(EC.presence_of_element_located((By.ID, "some_element_id"))) print("element: "+str(time.time()-start_time)) browser.quit()
Output
$ python3 test.py submit: 2.046938896179199 wait: 7.495832920074463 readyState: 7.501171588897705 staleness: 45.919543743133545 element: 45.93822622299194
由上述的結果可以看出來 readyState 讀出來的結果還是原來的網頁,應該要等個幾秒讓網頁跳轉後才能用 readyState 來判斷。
最終還是要使用 html.id 來判斷比較正確。
留言
張貼留言