html代码:
frame.html:
<html>
<meta http-equiv="Content-Type"content="text/html;charset=utf-8"/>
<head>
<title>frameset页面</title>
</head>
<frameset cols="25%,50%,25%">
<frame id = "leftframe"src="frame_left.html"/>
<frame id ="middleframe"src="frame_middleframe.html"/>
<frame id="rightframe"src="frame_right.html"/>
</frameset>
</html>
frame_left.html:
<html>
<meta http-equiv="Content-Type"content="text/html;charset=utf-8">
<head>
<script type="text/javascript">
function display_alert()
{
alert("i am an alert box!!")
}
</script>
</head>
<body>
<p>这是左侧frame页面上面的文字</p>
<input type="button" onclick="display_alert()"value="Display alert box"/>
</body>
</html>
frame_middleframe.html:
<html>
<meta http-equiv ="Content-type content="text/html;charset=utf-8"/>
<head>
<title>中间frame</title>
</head>
<body>
<p>这是中间frame页面上的文字</p>
<input type="text" id="text" value="">文本框</input>
</body>
</html>
frame_right.html:
<html>
<meta http-equiv="Content-Type "content="text/html;charset=utf-8"/>
<head>
<title>右侧frame</title>
</head>
<body>
<p>这是右侧frame页面上的文字</p>
<input id="python" type='radio'name="book"checked>python selenium </input>
<br/>
<input id="java" type='radio'name="book">java selenium</input>
</body>
</html>
实例:
#encoding=utf-8
from selenium import webdriver
import unittest
class VisitByFirefox(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox(executable_path="f:\\geckodriver")
def test_HandleFrame(self):
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
url="file:///F:/10.41/frame.html"
self.driver.get(url)
self.driver.switch_to.frame(0)
leftFrameText=self.driver.find_element_by_xpath("//p")
self.driver.find_element_by_tag_name("input").click()
try:
alertWindow=WebDriverWait(self.driver,10).until(EC.alert_is_present())
print(alertWindow.text)
alertWindow.accept()
except TimeoutException as e:
print(e)
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_elements_by_tag_name("frame")[1])
assert "这是中间frame页面上的文字"in self.driver.page_source
self.driver.find_element_by_tag_name("input").send_keys("我在中间frame")
self.driver.switch_to.default_content()
self.driver.switch_to.frame(self.driver.find_element_by_id("rightframe"))
assert "这是右侧frame页面上的文字"in self.driver.page_source
self.driver.switch_to.default_content()
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
使用Frame中的HTML源码内容操作Frame
实例:
#encoding=utf-8
from selenium import webdriver
import unittest
class VisitByFirefox(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox(executable_path="f:\\geckodriver")
def test_HandleFrameByPageSource(self):
url="file:///F:/10.41/frame.html"
self.driver.get(url)
framesList=self.driver.find_elements_by_tag_name("frame")
for frame in framesList:
self.driver.switch_to.frame(frame)
if "中间frame"in self.driver.page_source:
p=self.driver.find_element_by_xpath("//p")
self.assertAlmostEqual("这是中间frame页面上的文字",p.text)
self.driver.switch_to.default_content()
break
else:
self.driver.switch_to.default_content()
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()