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