Selenium - upload file to iframe

亡梦爱人 提交于 2019-12-04 17:04:28

Lets try this by giving some time for iframe to load by inserting the below code

Import time

## Give time for iframe to load ##
time.sleep(xxx)

hope this will work

alecxe

I would use an Explicit Wait and wait until the count of frame elements with name="upload_iframe" becomes 4 by writing a custom expected condition:

from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.support import expected_conditions as EC

class wait_for_n_elements(object):
    def __init__(self, locator, count):
        self.locator = locator
        self.count = count

    def __call__(self, driver):
        try:
            count = len(EC._find_elements(driver, self.locator))
            return count == self.count
        except StaleElementReferenceException:
            return False

Usage:

wait = WebDriverWait(driver, 10)
wait.until(wait_for_n_elements((By.NAME, 'upload_iframe'), 4))

driver.switch_to.frame(driver.find_elements_by_name("upload_iframe")[3])  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!