selenium 之操作frame
HTML文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>操作iframe</title>
</head>
<body>
<h2>IFRAME Tests</h2>
<iframe id="iframe1" name="name1" src="https://cn.bing.com/" height="300px" style="float:left;margin:20px;">
</iframe>
<div id="another" style="float:left;margin:20px;">
</div>
<script>
function writeAnotherIframe(){
document.getElementById("another").innerHTML = '<iframe name="name2" src="https://cn.bing.com/" height="300px" ></iframe>';
}
window.setTimeout("writeAnotherIframe()", 1000)
</script>
</body>
</html>
from selenium import webdriver
from time import sleep
import os
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome()
html_file = "file:///" + os.getcwd() + os.sep + "myhtml6-8.html"
driver.get(html_file)
# 通过id切换到frame
# driver.switch_to.frame("iframe1")
# 通过索引切换到frame,第一个是0
# driver.switch_to.frame(0)
# 通过name切换到frame
# driver.switch_to.frame("name2")
# 通过元素切换
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, '#iframe1'))
el = driver.find_element(By.CSS_SELECTOR, '#sb_form_q')
el.send_keys('张飞')
sleep(2)
driver.quit()
本文来自博客园,作者:chuangzhou,转载请注明原文链接:https://www.cnblogs.com/czzz/p/15923672.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!