3

I am currently scraping this website.

The site has google maps iframe embedded in it. It refreshes every time I enter an address in the search box. How do I check until the map finish refreshing?

I am currently using:

 WebDriverWait(driver, 120).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".gm-style > iframe:nth-child(2)")))

I'm unsure if this works because my internet is too fast for me to test it out.

I have checked the definitions of waits in the selenium website but I do not understand what they mean.

I also found a post which uses the same method but unsure if it waits for the iframe to load initially only or it waits for it to change as well.

2 Answers 2

2

As per your question if you intend to switch to the <iframe> identified as (By.CSS_SELECTOR, ".gm-style > iframe:nth-child(2)") you need to induce WebDriverWait for the <iframe> to be available and switch to it and you can use the following solution:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,".gm-style > iframe:nth-child(2)")))
Sign up to request clarification or add additional context in comments.

Comments

1

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_...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.