如何在 Python 中的 Selenium Webdriver 中处理框架?
我们可以在 Python 的 Selenium webdriver 中处理框架。iframe 在 html 文档中用 <iframe> 标记标识。iframe 是包含位于另一个 html 文档中元素的 html 文档。让我们看看一个框架的 html 文档。

以下方法有助于在 iframe 之间切换 −
switch_to.frame(args) – 框架索引作为参数放入方法中。iframe 的开始索引为 0。
语法
driver.switch_to.frame(0),切换到第一个 iframe。
switch_to.frame(args) - 框架名称或 ID 作为参数放入方法中。
语法
driver.switch_to.frame("nm"),切换到名为 nm 的 iframe。
- switch_to.frame(args) - 框架 web 元素作为参数放入方法中。
语法
driver.switch_to.frame(f),切换到带 web 元素 f 的 iframe。
switch_to.default_content() – 从 iframe 切换到父页面。
语法
driver.switch_to.default_content()
示例
实现代码。
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
driver.get("https://the-internet.herokuapp.com")
driver.find_element_by_link_text("Frames").click()
driver.find_element_by_link_text("Nested Frames").click()
# switch to frame with name
driver.switch_to.frame("frame-bottom")
# identify the element and get text method
s = driver.find_element_by_xpath("//body").text
print ("Test inside frame: " + s)
# move out of frame to the parent page
driver.switch_to.default_content()
driver.quit()输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP