Python+Selenium如何實現從本地選擇文件上傳可難爲了我這個小菜鳥。通過查閱資料發現可以藉助Autolt工具實現,因爲是剛開始學,也不確定這種方式是不是最好的==,僅供大家參考
1.使用Autolt創建應用程序
1.下載並安裝Autoit
https://www.autoitscript.com/cgi-bin/getfile.pl?autoit3/autoit-v3-setup.exe
2.安裝成功後打開AutoIt Windows Info識別彈框信息
3.打開SciTE Script Editor,根據彈框編寫腳本
WinActivate("打開");
ControlSetText("打開", "", "Edit1", "F:\個人文檔\素材\素材\小icon\momo.jpg" );
Sleep(2000);
ControlClick("打開", "", "Button1");
4.Compile Script to.exe 用於將AutoIt生成執行文件
該步驟成功執行後,生成文件modifyPic.exe
5.Run Script 用於執行AutoIt腳本驗證功能是否生效
如果生效,則開始編寫代碼
2.在代碼中引用
以下是csdn上傳頭像實例
# -*- coding: utf-8 -*-
#sample02.py
import configparser
import os
from selenium import webdriver
import time
PostUrl = "https://i.csdn.net/#/uc/profile"
# 用瀏覽器實現訪問
driver = webdriver.Chrome()
driver.get(PostUrl)
time.sleep(3)
driver.find_element_by_xpath('/html/body/div[4]/div/div/div[2]/div/h3/a').click()
time.sleep(3)
driver.find_element_by_id('username').send_keys("******@163.com")
driver.find_element_by_id('password').send_keys("******")
driver.find_element_by_class_name('logging').click()
time.sleep(5)
driver.find_element_by_class_name("modify").click()
driver.find_element_by_class_name("vicp-drop-area").click()
time.sleep(2)
os.system("F:\\script\\modifyPic.exe")
time.sleep(5)
driver.find_element_by_class_name("vicp-operate-btn").click()