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()
操作IFrame中的页面元素
修改上面的frame_left.html
<html>
<meta http-equiv="Content-Type"content="text/html;charset=utf-8"/>
<head>
<title>左侧frame</title>
<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"/>
<iframe id="showiframe"src='iframe.html'style="width:200px";"height:50px"></iframe>
</body>
</html>
将iframe.html与frame_left.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_HandleIFrame(self):
url="file:///F:/10.41/frame.html"
self.driver.get(url)
self.driver.switch_to.frame(0)
assert "这是左侧frame页面上的文字"in self.driver.page_source
self.driver.switch_to.frame(self.driver.find_element_by_id("showiframe"))
assert "这是iframe页面上的文字"in self.driver.page_source
self.driver.switch_to.default_content()
assert "frameset页面" ==self.driver.title
self.driver.switch_to.frame(1)
assert "这是中间frame页面上的文字"in self.driver.page_source
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
操作JavaScript的confirm弹窗
html代码:
<html>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<head>
<title>你喜欢的水果</title>
</head>
<body>
<input id='button'onclick="alert('这是一个alert弹出框');"value='单击此按钮,弹出alert弹窗'/>
</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_HandleAlert(self):
from selenium.common.exceptions import NoAlertPresentException
import time
url="file:///F:/14.html"
self.driver.get(url)
button=self.driver.find_element_by_id("button")
button.click()
try:
alert=self.driver.switch_to.alert
time.sleep(2)
self.assertEqual(alert.text,"这是一个alert弹出框")
alert.accept()
except NoAlertPresentException as e:
self.fail("尝试操作的alert框未被找到")
print(e)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
操作Javascript的confrim弹窗
html代码:
<html>
<meta http-equiv="Content-Type"content="text/html;charset=utf-8"/>
<head>
<title>你喜欢的水果</title>
</head>
<body>
<input id='button'type='button'onclick="confirm('这是一个confirm弹出框');"
value='单击此按钮,弹出confirm弹出窗'/>
</input>
</body>
</html>
API实例代码:
#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_Handleconfirm(self):
from selenium.common.exceptions import NoAlertPresentException
import time
url="file:///F:/15.html"
self.driver.get(url)
button=self.driver.find_element_by_id("button")
button.click()
try:
alert=self.driver.switch_to.alert
time.sleep(2)
self.assertEqual(alert.text,"这是一个confirm弹出框")
alert.accept()
except NoAlertPresentException as e:
self.fail("尝试操作的confirm框未被找到")
print(e)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
操作Javascript的prompt弹窗:
html代码:
<html>
<meta http-equiv="Content-Type"content="text/html;charset=utf-8"/>
<head>
<title>你喜欢的水果</title>
</head>
<body>
<input id ='button'type='button'onclick="prompt('这是一个prompt弹出框');"
value='单击此按钮,弹出prompt弹出框'/>
</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_HandlePrompt(self):
url="file:///F:/16.html"
self.driver.get(url)
element=self.driver.find_element_by_id("button")
element.click()
import time
time.sleep(2)
alert=self.driver.switch_to.alert
self.assertEqual("这是一个prompt弹出框",alert.text)
time.sleep(1)
alert.send_keys("光荣之路")
time.sleep(1)
alert.accept()
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
操作浏览器cookie:
#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_Cookies(self):
url="https://www.sogou.com"
self.driver.get(url)
cookies=self.driver.get_cookies()
for cookie in cookies:
print("%s->%s->%s->%s->%s"%(cookie['domain'],cookie["name"],cookie["value"],cookie["expiry"],cookie["path"]))
ck = self.driver.get_cookie("SUV")
print("%s->%s->%s->%s->%s" % (ck['domain'], ck["name"], ck["value"], ck["expiry"], ck["path"]))
print(self.driver.delete_cookie("ABTEST"))
#self.driver.delete_all_cookies()
cookies=self.driver.get_cookies()
print(cookies)
self.driver.add_cookie({"name":"gloryroadTrain",'value':'1479697159269020'})
cookie=self.driver.get_cookie("gloryroadTrain")
print(cookie)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
没明白这个要干嘛