import unittest
from time import sleep
import os
from selenium import webdriver
import win32api
import win32con
import win32clipboard as cut
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
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/")
self.driver.implicitly_wait(15)
def test_uploadPicture(self):
self.driver.find_element_by_xpath(".//*[@id='form']/span[1]/span").click()
# 点击百度相机图标
WebDriverWait(self.driver, 15).until(
lambda x: x.find_element_by_xpath(".//*[@id='form']/div/div[2]/div[2]/input"))
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("ctrl")
keyUp("v")
# 松开Ctrl+V
keyDown("enter")
# 按下回车键
keyUp("enter")
# 松开回车键
sleep(8)
searchResultTitle = self.driver.title
baiduImageTitle = "百度识图——以图搜信息,发现更多可能"
WebDriverWait(self.driver, 15).until(EC.title_is(baiduImageTitle))
self.assertEqual(searchResultTitle, baiduImageTitle)
def tearDown(self):
self.driver.close()
self.driver.quit()
if __name__ == '__main__':
unittest.main()