#截图功能
#-*-coding=utf-8
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import unittest
import time
class login(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(10)
self.base_url = "http://www.baidu.com"
self.verificationErrors = []
def test_loginpass(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_link_text("搜索设置").click()
driver.find_element_by_id("s1_2").click()
m=driver.find_element_by_name("NR")
try:
m.find_element_by_xpath("//option[@value='1000']").click()
except:
driver.get_screenshot_as_file("D:/screenshots/dd.png")#如果没有找到上面的元素就截取当前页面。
time.sleep(5)
driver.find_element_by_xpath("//input[@value='保存设置']").click()
driver.switch_to_alert().accept()
def tearDown(self):
#self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
python webdriver 截图函数为get_screenshot_as_file(filename)
也可以事先定义好文件名和路径