My test works fine on chromedriver and IEServerDriver. But it doesn't work on safaridriver which comes with Safari10 (/usr/bin/safaridriver). On SafariDriver, it can find the 1st and the 2nd level iframes. But it cannot find the 3rd level iframe although it is there. Please find HTML, Code, and Output from terminal below.
Platform: macOs Sierra version 10.12 / Safari Version 10.0 (12602.1.50.0.10) / Python 2.7.12 / selenium 3.0.1
HTML:
<body style="background: black;">
<!-- Technical Info -->
<!-- Some Info -->
<iframe name="AppFrame" src="path-to-an-app" seamless="" height="100%" width="100%" frameborder="0">
<html><head></head>
<body>
<iframe id="contentframe" name="internal" class="contentFrame" iframeid="fp1478845052175">
<html><head></head>
<body>
<!-- Technical Info -->
<!-- Some Info of inner app -->
<iframe name="AppFrame" src="path-to-another-app" seamless="" height="100%" width="100%" frameborder="0"></iframe>
...
</iframe>
Code:
driver.switch_to_frame(driver.find_element_by_name("AppFrame"))
print("Switch to 1st iframe")
time.sleep(1)
driver.switch_to_frame(driver.find_element_by_id("contentframe"))
print("Switch to 2nd iframe")
time.sleep(1)
print("body: " + driver.find_element_by_tag_name("body").get_attribute("outerHTML"))
try:
appframe = driver.find_element_by_name("AppFrame")
except:
print("body in except: " + driver.find_element_by_tag_name("body").get_attribute("outerHTML"))
driver.switch_to_frame(driver.find_element_by_name("AppFrame"))
print("Switch to 3rd iframe")
print("body again: " + driver.find_element_by_tag_name("body").get_attribute("outerHTML"))
Output:
Switch to 1st iframe
Switch to 2nd iframe
body: <body>
<!-- Technical Info -->
<!-- Some Info -->
<iframe name="AppFrame" src="path-to-another-app" seamless="" height="100%" width="100%" frameborder="0"></iframe>
</body>
body in except: <body>
<!-- Technical Info -->
<!-- Some Info -->
<iframe name="AppFrame" src="path-to-another-app" seamless="" height="100%" width="100%" frameborder="0"></iframe>
</body>
Traceback (most recent call last):
File "testSafariDriver.py", line 34, in <module>
driver.switch_to_frame(driver.find_element_by_name("AppFrame"))
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 365, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 752, in find_element
'value': value})['value']
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.
I've tried this on C#, it can find the iframe but I cannot switch to it and got the error message "stale element" although I can access its attribute such as outerHTML, position, src, width.