首页 > 其他分享 >使用selenium.webdriver操作无头模式--haedless浏览器上传文件

使用selenium.webdriver操作无头模式--haedless浏览器上传文件

时间:2023-01-30 15:46:07浏览次数:56  
标签:webdriver -- selenium driver token file 上传 find

第一种情况

Selenium 官方文档的方法:文件上传 | Selenium

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(10)
driver.get("https://the-internet.herokuapp.com/upload");
driver.find_element(By.ID,"file-upload").send_keys("selenium-snapshot.jpg")
driver.find_element(By.ID,"file-submit").submit()
if(driver.page_source.find("File Uploaded!")):
    print("file upload success")
else:
    print("file upload not successful")
driver.quit()

这种方法对于input格式的文件上传格式适用,只要定位到file-upload的input框,然后 send_keys 输入文件路径,click上传即可。

第二种情况:

对点击上传后弹出windows窗口的文件上传,如果是在wiindow上运行的程序可采用autoit或者robot的方法,但是想在linux上采用无头浏览器上传文件显然不行,因为没有UI界面

先说结论,目前没有直接的方法,转换思路

我采用的是先登录系统,然后拿到token后,使用curl 带token参数上传文件,在linux上非常好用,前提是需要利用浏览器F12找到文件上传的接口。

代码如下:

def upload_log(file_path):
    '''上传日志文件到 Reportportal'''
    # 模拟superadmin用户登录,获取token
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')   # 采用无头模式
    chrome_options.add_argument('--no-sandbox')
    driver = webdriver.Chrome('/usr/bin/chromedriver', chrome_options=chrome_options)
    driver.get('http://10.8.8.9:8080/ui/#login')
    driver.find_element_by_name('login').send_keys('admin')
    driver.find_element_by_name('password').send_keys('admin')
    driver.find_element_by_xpath('//button[contains(text(), "Login")]').click()
    time.sleep(5)
    token = driver.execute_script('return localStorage.getItem("token");')
    token_type = token[0]['type']
    token_value = token[1]['value']

    # 上传文件
    Rpurl = "http://10.8.8.9:8080/api/v1/superadmin_personal/launch/import"
    command = 'curl -H "Accept: application/json" -H "Authorization: %s %s" -F "file=@%s" -X POST "%s"' % (token_type, token_value, file_path, Rpurl)
    p = os.popen(command).readlines()
    print(p)
    if 'successfully imported' in p[0]:
        print('日志已上传网站')
    else:
        print('日志上传失败')
    driver.close()

  

 

标签:webdriver,--,selenium,driver,token,file,上传,find
From: https://www.cnblogs.com/wushua/p/17076154.html

相关文章

  • 无声音频 空白音频 ios后台运行保活
    1为了实现后台定时器继续循环 用到了 https://blog.csdn.net/qq_38520096/article/details/102626210用播放音频的方法播放无声音频实现2功能实现了但是没找到无声音......
  • drf从入门到精通(01)
    今日内容目录今日内容web应用模式nullAPI接口接口测试工具postmannullweb应用模式#djangoweb框架专门用来写web项目#之前学的,写的bbs项目,图书管理系统,用的......
  • Fitter-细节-过滤器拦截方式配置 Fitter-细节-过滤器链(多个过滤器)
    Fitter-细节-过滤器拦截方式配置  拦截方式配置:资源被访问的方式注解配置:设置dispatcherTypes属性1.REQUEST:默认值。浏览器直接请求资源......
  • 邮件-原理(SMTP/POP3 或 SMTP/IMAP)
    什么是SMTP?SMTP基于TCP/IP。SMTP是电子邮件发送的行业标准协议。所以使用SMTP协议可以向SMTP服务器发送、中继或转发邮件。什么是SMTP服务器?SMTP服务器用于发送邮件......
  • c++线程池
    #pragmaonce#include<algorithm>#include<atomic>#include<condition_variable>#include<cstdint>#include<functional>#include<future>#include<map>#i......
  • mysql8.0远程访问配置
    mysql8.0远程访问配置一、修改bind-address查找配置文件my.confwhichmysql/usr/bin/mysql--verbose--help|grep-A1'Defaultoptions'vimy.cnfbind-addres......
  • EFCore build failure
    今日学习源代码,里面按照业务划分了6个微服务,挨个执行add-migrationinit时提示buildfailure,无其他任何提示。Ctrl+Shift+B生成解决方案后显示出是另外一个类库的问题,......
  • jQuery_1_封装原理
    jQuery是使用JavaScript 编写的函数库,用于简化了JavaScript编程。 问题:在一个页面中,无论是引入外部的js代码,还是内部分开写的js代码块,在此页面解析执行的时候,......
  • 汽车虚拟仿真,实时云渲染至关重要!
    什么是实时云渲染、汽车虚拟仿真?实时云渲染技术是一种基于云计算技术的图形渲染技术,它通过云端的运算能力来实现实时的图形渲染。其发展历程始于20世纪90年代末,随着云计算......
  • 鼎捷ERP二维码整体解决方案 Tiptop GP条码管理系统 鼎捷ERP移动解决方案 鼎捷条码扫描
    本人在ERP实施公司做顾问四五年,参与企业实施ERP十多个项目,非常熟悉企业ERP流程,在实施过程遇到众多问题,提出了不少根据企业具体情况的解决方案。     最近定制开......