我的测试在chromedriver和IEServerDriver上运行正常。但它不适用于Safari10附带的safaridriver(/ usr / bin / safaridriver)。在SafariDriver上,它可以找到第一级和第二级iframe。但它找不到第3级iframe,尽管它在那里。请从下面的终端找到HTML,代码和输出。
平台: macOs Sierra版本10.12 / Safari版本10.0(12602.1.50.0.10)/ Python 2.7.12 / selenium 3.0.1
HTML:
johnson
代码:
<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>
输出:
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"))
我在C#上尝试了这个,它可以找到iframe,但我无法切换到它并得到错误消息“陈旧元素”,虽然我可以访问其属性,如outerHTML,position,src,width。