Python SELENIUM上傳文件或圖片實(shí)現(xiàn)過程
逛網(wǎng)站的時(shí)候經(jīng)常會遇到需要上傳圖片的操作,這里主要來說下selenium操作上傳文件的操作。
前提條件:定位的元素必須是type 屬性是file類型。即type="file",如下圖:

詳細(xì)用法:

參考代碼:
from selenium import webdriver
import time
driver = webdriver.Chrome()
def test_open_page():
'''打開界面'''
driver.maximize_window()
driver.get('http://106.233.81.250/fw/index.php?ctl=user&act=login')
def test_register(user,password):
'''輸入用戶名密碼'''
driver.find_element_by_css_selector('#login-email-address').send_keys(user)
driver.find_element_by_css_selector('#login-password').send_keys(password)
driver.find_element_by_xpath('//input[@type="submit"]').click()
driver.implicitly_wait(30)
driver.find_element_by_xpath('//input[@value="取消"]').click()
def test_money():
'''操作我要借款'''
#driver.find_element_by_link_text('我要借款').click()
el = driver.find_elements_by_css_selector('.pr20')
el[2].click()
ele = driver.find_elements_by_xpath('//div[@class="tc pt10"]/a/img')
ele[0].click()
def test_input_info():
'''輸入借款信息'''
driver.find_element_by_css_selector('#borrowtitle').send_keys(2)
driver.find_element_by_css_selector('#borrowamount').send_keys(2000)
driver.find_element_by_css_selector('#repaytime').send_keys(20)
driver.find_element_by_css_selector('#apr').send_keys(20)
# todo js操作滾動條
js1 = "document.documentElement.scrollTop=1000"
driver.execute_script(js1)
time.sleep(1)
elem = driver.find_elements_by_xpath('//div[@style="width:710px;"]/input[@type="text"]')
elem[0].send_keys(2)
driver.find_element_by_xpath('//button[@rel="file_1"]').click()
time.sleep(1)
driver.find_element_by_xpath('//li[text()="本地上傳"]').click()
time.sleep(1)
# todo 文件操作上傳圖片
driver.find_element_by_name('imgFile').send_keys(r'D:\file\1.png')
time.sleep(1)
driver.find_element_by_xpath('//input[@type="button" and @value="確定"]').click()
def test_closed():
time.sleep(6)
driver.quit()
test_open_page()
test_register('admin','admin')
test_money()
test_input_info()
test_closed()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Selenium瀏覽器自動化如何上傳文件
- selenium+python實(shí)現(xiàn)文件上傳操作的方法實(shí)例
- Python爬蟲中Selenium實(shí)現(xiàn)文件上傳
- Python selenium文件上傳下載功能代碼實(shí)例
- 基于python的selenium兩種文件上傳操作實(shí)現(xiàn)詳解
- python+selenium+autoit實(shí)現(xiàn)文件上傳功能
- Python中selenium實(shí)現(xiàn)文件上傳所有方法整理總結(jié)
- Python selenium文件上傳方法匯總
- Python中Selenium上傳文件的幾種方式
相關(guān)文章
Python實(shí)現(xiàn)將數(shù)據(jù)寫入netCDF4中的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)將數(shù)據(jù)寫入netCDF4中的方法,涉及Python數(shù)據(jù)處理與文件讀寫相關(guān)操作技巧,需要的朋友可以參考下2018-08-08
Django 實(shí)現(xiàn)購物車功能的示例代碼
這篇文章主要介紹了Django 實(shí)現(xiàn)購物車功能的示例代碼,實(shí)現(xiàn)了刪除產(chǎn)品和顯示購物車的一系列購物車的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
如何基于python3和Vue實(shí)現(xiàn)AES數(shù)據(jù)加密
這篇文章主要介紹了如何基于python3和Vue實(shí)現(xiàn)AES數(shù)據(jù)加密,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
PyQt5中QSpinBox計(jì)數(shù)器的實(shí)現(xiàn)
這篇文章主要介紹了PyQt5中QSpinBox計(jì)數(shù)器的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
PyQt5使用QTimer實(shí)現(xiàn)電子時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了PyQt5使用QTimer實(shí)現(xiàn)電子時(shí)鐘,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07

