Selenium(Python)上傳圖片

來源:互聯網
上載者:User

標籤:inpu   tle   project   unit   __name__   setup   elf   equal   tca   

import unittest
from time import sleep

import os
from selenium import webdriver
import win32api
import win32con
import 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()

Selenium(Python)上傳圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.