操作可以输入的下拉框(输入同时模拟按键)
html代码
<html>
<body>
<div style="position:relative;">
<input list="pasta"id="select">
<datalist id="pasta">
<option>Bavette</option>
<option>Rigatoni</option>
<option>Fiorentine</option>
<option>Gnocchi</option>
<option>Tagliatelle</option>
<option>Penne</option>
<option>Pici</option>
<option>Pappardelle</option>
<option>Spaghetti</option>
<option>Cannelloni</option>
<option>Cancl</option>
</datalist>
</div>
</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_oprateMultipleOptionDropList(self):
url="file:///F:/7.html"
self.driver.get(url)
from selenium.webdriver.common.keys import Keys
self.driver.find_element_by_id("select").clear()
import time
time.sleep(1)
self.driver.find_element_by_id("select").send_keys("c",Keys.ARROW_DOWN)
self.driver.find_element_by_id("select").send_keys(Keys.ARROW_DOWN)
self.driver.find_element_by_id("select").send_keys(Keys.ENTER)
time.sleep(2)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
这里用了Keys模块:模拟键盘事件的模块
操作单选框
html代码:
<html>
<meta charset="UTF-8">
<body>
<form>
<input type="radio"name="fruit"value="berry"/>草莓</input>
<br/>
<input type="radio"name="fruit"value="watermelon"/>西瓜</input>
<br/>
<input type="radio"name="fruit"value="organe"/>橙子</input>
</form>
</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_oprateRadio(self):
url="file:///F:/8.html"
self.driver.get(url)
berryRadio=self.driver.find_element_by_xpath("//input[@value='berry']")
berryRadio.click()
self.assertTrue(berryRadio.is_selected(),"草莓单选框未被选中!")
if berryRadio.is_selected():
watermelonRadio=self.driver.find_element_by_xpath("//input[@value='watermelon']")
watermelonRadio.click()
self.assertFalse(berryRadio.is_selected())
radioList=self.driver.find_elements_by_xpath("//input[@name='fruit']")
'''循环遍历radioList中的每个按钮,查找value属性值为“orange”的单选框
如果找到后发现处于未选中状态,则调用click方法选中
'''
for radio in radioList:
if radio.get_attribute("value")=="orange":
if not radio.is_selected():
radio.click()
self.assertEqual(radio.get_attribute("value"),"orange")
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
element这个东西,一定要注意单复数
操作复选框
html代码:
<html>
<body>
<meta charset="UTF-8">
<form name='from1'>
<input type="checkbox"name="fruit"value="berry"/>草莓</input>
<br/>
<input type="checkbox"name="fruit"value="watermelon"/>西瓜</input>
<br/>
<input type="checkbox"name="fruit"value="orange"/>橙子</input>
</form>
</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_oprateCheckBox(self):
url="file:///F:/9.html"
self.driver.get(url)
berryCheckBox=self.driver.find_element_by_xpath("//input[@value='berry']")
berryCheckBox.click()
self.assertTrue(berryCheckBox.is_selected(),"草莓复选框未被选中")
if berryCheckBox.is_selected():
berryCheckBox.click()
self.assertFalse(berryCheckBox.is_selected())
checkBoxList=self.driver.find_elements_by_xpath("//input[@name='fruit']")
for box in checkBoxList:
if not box.is_selected():
box.click()
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
断言页面源码中的关键字
#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_assertKeyWord(self):
url="https://www.baidu.com"
self.driver.get(url)
self.driver.find_element_by_id("kw").send_keys("光荣之路自动化测试")
self.driver.find_element_by_id("su").click()
import time
time.sleep(4)
assert u"首页--光荣之路"in self.driver.page_source,u"页面源码中不存在该关键字"
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
对当前浏览器窗口截屏
#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_captureScreenInCurrentWindow(self):
url="https://www.sogou.com"
self.driver.get(url)
try:
'''调用get_screenhot_as_file(filename)方法,对浏览器当前打开页面进行截图,并保存为C盘下的screenPicture.png文件'''
result=self.driver.get_screenshot_as_file(r"f:\screenPicture.png")
print(result)
except IOError as e:
print(e)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
拖拽页面元素
#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_dragPageElement(self):
url="https://jqueryui.com/resources/demos/draggable/scroll.html"
self.driver.get(url)
initialPosition=self.driver.find_element_by_id("draggable")
targetPosition=self.driver.find_element_by_id("draggable2")
dragElement=self.driver.find_element_by_id("draggable3")
from selenium.webdriver import ActionChains
import time
'''创建一个新的ActionChains,将webdriver实例对象driver作为参数值传入,通过webdruver实例执行用户操作'''
action_chains=ActionChains(self.driver)
action_chains.drag_and_drop(initialPosition,targetPosition).perform()
for i in range(5):
action_chains.drag_and_drop_by_offset(dragElement,10,10).perform()
time.sleep(2)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
模拟键盘单个按键操作
#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_siumlateASingleKeys(self):
url="https://www.sogou.com"
self.driver.get(url)
from selenium.webdriver.common.keys import Keys
import time
query=self.driver.find_element_by_id("query")
query.send_keys(Keys.F12)
time.sleep(3)
query.send_keys(Keys.F12)
query.send_keys("selenium")
query.send_keys(Keys.ENTER)
time.sleep(3)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()