import unittestfrom time import sleep import osfrom selenium import webdriverimport win32apiimport win32conimport win32clipboard as cut def shearPlate(copyString): cut.OpenClipboard() cut.EmptyClipboard() cut.SetClipboardData(win32con.CF_UNICODETEXT, copyString) cut.CloseClipboard() # 设置剪切板内容 VK_CODE = { "ctrl":17, "v":86, "enter":13}# 键盘映射字典 def keyDown(keyName): win32api.keybd_event(VK_CODE[keyName], 0, 0, 0) # 按下按键 def keyUp(keyName): win32api.keybd_event(VK_CODE[keyName], 0, win32con.KEYEVENTF_KEYUP, 0) # 松开按键 class UploadPicture(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.driver.maximize_window() self.driver.get("https://www.baidu.com/") sleep(2) def test_uploadPicture(self): self.driver.find_element_by_xpath(".//*[@id=‘form‘]/span[1]/span").click() # 点击百度相机图标 sleep(2) self.driver.find_element_by_xpath(".//*[@id=‘form‘]/div/div[2]/div[2]/input").click() # 点击本地上传图片 sleep(2) projectPath = os.getcwd() # 获取当前工程的路径 shearPlate(projectPath + "\\img.jpg") # 把图片路径复制粘贴到剪切板中 keyDown("ctrl") keyDown("v") # 按下Ctrl+V keyUp("v") keyUp("ctrl") # 松开Ctrl+V keyDown("enter") # 按下回车键 keyUp("enter") # 松开回车键 sleep(5) searchResultTitle = self.driver.title baiduImageTitle = "百度识图——以图搜信息,发现更多可能" self.assertEqual(searchResultTitle, baiduImageTitle) def tearDown(self): self.driver.close() self.driver.quit() if __name__ == ‘__main__‘: unittest.main()
原文地址:https://www.cnblogs.com/yjlch1016/p/8470484.html
时间: 2024-10-31 07:56:32