Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
827 views
in Technique[技术] by (71.8m points)

python - Why does Selenium's wait.until_not(EC.invisibility_of_element_located) wait for too long for a loader to disappear?

Which selenium.webdriver.support.expected_conditions are better to use when waiting for the invisibility of an element? In my case, I input data into a form, click save and wait for a loader to disappear

from selenium.webdriver.support import expected_conditions as EC 
wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((SelectBy.CSS_SELECTOR, ".spinner")))
debug("loader appeared")
wait.until(EC.invisibility_of_element_located((SelectBy.CSS_SELECTOR, ".spinner")))
debug("loader disappeared")

In the output, I see that the second wait is executed for 20 seconds (my global implicit wait is 20 seconds)

360ms ?     [debug] loader appeared
21s 141ms ? [debug] loader disappeared

The locator is good, I am trying to understand what is wrong with the wait. Did anyone have similar problems? I would be happy for any suggestions.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your wait operations are stacking on each other because your code is chained.

Let me explain with your code:

# 1. create a wait object
wait = WebDriverWait(driver, 10)

# 2. execute a wait statement
wait.until(EC.presence_of_element_located((SelectBy.CSS_SELECTOR, ".spinner")))
debug("loader appeared")

# 3. execute a wait statement
wait.until(EC.invisibility_of_element_located((SelectBy.CSS_SELECTOR, ".spinner")))
debug("loader disappeared")

Both wait statements (#2 and #3) are using the same wait object, so their execution will "stack":

  1. Wait object will wait 10 seconds for a condition
  2. Wait 10 seconds (inherited) + spinner appear
  3. Wait 10 seconds (inherited) + spinner appear (inherited) + spinner disappear

Action #2 waits 10s. Action #3 waits 20s.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...