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()

https://blog.csdn.net/huilan_same/article/details/52200586?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-2.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-2.pc_relevant_default&utm_relevant_index=4

posted @ 2022-02-22 16:16  chuangzhou  阅读(68)  评论(0编辑  收藏  举报