Python SELENIUM上传文件或图片实现过程

yipeiwu_com6年前Python基础

逛网站的时候经常会遇到需要上传图片的操作,这里主要来说下selenium操作上传文件的操作。

前提条件:定位的元素必须是type 属性是file类型。即type="file",如下图:

详细用法:

参考代码:

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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python2随机数列生成器简单实例

本文实例讲述了Python2随机数列生成器。分享给大家供大家参考,具体如下: #filename:randNumber.py import random while True:...

python中map的基本用法示例

python中map的基本用法示例

map()函数 map() 会根据提供的函数对指定序列做映射,是内置函数 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 functi...

python3.6.3安装图文教程 TensorFlow安装配置方法

python3.6.3安装图文教程 TensorFlow安装配置方法

本文主要介绍Python3.6及TensorFlow的安装和配置流程。 一、Python官网下载自己电脑和系统对应的Python安装包。  网址:下载地址 一直往下拉到Fil...

对TensorFlow中的variables_to_restore函数详解

variables_to_restore函数,是TensorFlow为滑动平均值提供。之前,也介绍过通过使用滑动平均值可以让神经网络模型更加的健壮。我们也知道,其实在TensorFlow...

对Python模块导入时全局变量__all__的作用详解

对Python模块导入时全局变量__all__的作用详解

Python中一个py文件就是一个模块,“__all__”变量是一个特殊的变量,可以在py文件中,也可以在包的__init__.py中出现。 1、在普通模块中使用时,表示一个模块中允许哪...