I have a page including iframe
tags, and want to catch if content is fully loaded in the iframe.
I use time.sleep with check document.readyState
and it works well in ideal cases ; strong and fast response from web server. But It seems not to guarantee all situations, and I want to improve my code.
Please tell me know some advice or tips. Thanks.
My envs
I refer below documents.
and i wrote code below
def wait_for_document(self, driver):
time.sleep(3)
for i in range(20):
if driver.execute_script("return document.readyState") == "complete" : return
else : time.sleep(1)
Try below code to wait for iframe
and switch to it to be able to handle inner nodes:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get(URL)
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("iframe_name_or_id"))
Instead of "iframe_name_or_id"
you can pass iframe
as WebElement:
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_xpath('//iframe')))
To wait for presence of element inside frame:
wait(driver, 10).until(EC.presence_of_element_located((By.ID, "Element ID")))
You can also use By.NAME
, By.CLASS_NAME
, By.XPATH
, etc...
More about ExplicitWait
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With