我的测试在 chromedriver 和 IEServerDriver 上运行良好。但它不适用于 Safari10 (/usr/bin/safaridriver) 附带的 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:
<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"))
输出:
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.
我已经在 C# 上尝试过,它可以找到 iframe,但我无法切换到它并收到错误消息“stale element”,尽管我可以访问它的属性,例如outerHTML、位置、src、宽度。