I have tried this code:
driver.get('https://www.bungol.ca/map/location/scarborough/?')
time.sleep(3)
refresh_count = 0
while True:
try:
driver.refresh()
# WebDriverWait(driver, 120).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".gm-style > iframe:nth-child(2)")))
el = driver.find_element_by_css_selector(".gm-style > iframe:nth-child(2)")
assert el.is_displayed()
refresh_count += 1
except Exception:
print("Refresh count = " + str(refresh_count))
And the output was:
Refresh count = 0
Refresh count = 43
Refresh count = 44
Refresh count = 56
Refresh count = 62
Refresh count = 63
Refresh count = 64
Refresh count = 68
Refresh count = 69
The iframe was not in the DOM at 0, 43, 44, ... . The first time it failed because of the long first load. Then it was good and I decided to simulate slow connection by toggling Chrome to mobile version. And then it failed already relative often.
And then I have set WebDriverWait:
while True:
try:
driver.refresh()
WebDriverWait(driver, 120).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".gm-style > iframe:nth-child(2)")))
el = driver.find_element_by_css_selector(".gm-style > iframe:nth-child(2)")
assert el.is_displayed()
refresh_count += 1
except Exception:
print("Refresh count = " + str(refresh_count))
and it didn't throw any exception. So you can use EC.presence_of_element_...