Python SELENIUM上傳文件或圖片實(shí)現(xiàn)過程
逛網(wǎng)站的時(shí)候經(jīng)常會(huì)遇到需要上傳圖片的操作,這里主要來說下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操作滾動(dòng)條 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瀏覽器自動(dòng)化如何上傳文件
- 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學(xué)習(xí)--使用QQ郵箱發(fā)送郵件代碼實(shí)例
這篇文章主要介紹了python使用QQ郵箱發(fā)送郵件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04python實(shí)現(xiàn)從字符串中找出字符1的位置以及個(gè)數(shù)的方法
這篇文章主要介紹了python實(shí)現(xiàn)從字符串中找出字符1的位置以及個(gè)數(shù)的方法,對于Python字符串操作的學(xué)習(xí)有一定的幫助與借鑒作用,需要的朋友可以參考下2014-08-08Python Django網(wǎng)頁界面協(xié)同過濾推薦算法實(shí)現(xiàn)商品管理與推薦
商品管理與推薦系統(tǒng),本系統(tǒng)使用Python作為主要開發(fā)語言,前端采用HTML、CSS、BootStrap等技術(shù)搭建顯示界面,后端采用Django框架處理用戶的請求響應(yīng)2023-11-11解決pycharm 格式報(bào)錯(cuò)tabs和space不一致問題
這篇文章主要介紹了解決pycharm 格式報(bào)錯(cuò)tabs和space不一致問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02如何利用Python解析超大的json數(shù)據(jù)(GB級(jí)別)
果不想從頭開始創(chuàng)建數(shù)據(jù)格式來存儲(chǔ)數(shù)據(jù),JSON是一個(gè)不錯(cuò)的選擇,下面這篇文章主要給大家介紹了關(guān)于如何利用Python解析超大的json數(shù)據(jù)(GB級(jí)別)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07PyTorch基礎(chǔ)之torch.nn.Conv2d中自定義權(quán)重問題
這篇文章主要介紹了PyTorch基礎(chǔ)之torch.nn.Conv2d中自定義權(quán)重問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02