Selenium+python上传本地文件或者图片
基于input标签的,有属性type = file:
首先定位到点击上传的元素(input)然后直接使用send_keys()将文件在本地的路径传进去
代码如下(例子来源于本页面上传图片和文件):
from selenium import webdriver from time import sleep url = \'https://i.cnblogs.com/\' dr = webdriver.Chrome() dr.get(url) dr.implicitly_wait(10) dr.find_element_by_id(\'input1\').send_keys(\'xxxxx\') dr.find_element_by_id(\'input2\').send_keys(xxxxxx\') dr.find_element_by_id(\'signin\').click() #按钮类型为input上传图片 # dr.find_element_by_css_selector(\'[href="/EditPosts.aspx?opt=1"]\').click() # dr.find_element_by_css_selector(\'[class="mceIcon"][alt="上传图片"]\').click() # dr.switch_to.frame(\'mce_39_ifr\') # dr.find_element_by_css_selector("input[type=\'file\']").send_keys(u\'E:\微信图片_20170710155107.png\') #上传文件 dr.find_element_by_id(\'TabFiles\').click() dr.find_element_by_id(\'AddFiles_myFile\').send_keys(\'E:\Config.xls\') sleep(10) dr.quit()
版权声明:本文为yy-cola原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。