【问题标题】:How to edit a file and save it using Python selenium or robotframework如何使用 Python selenium 或 robotsframework 编辑和保存文件
【发布时间】:2022-06-11 04:15:53
【问题描述】:

我有以下自动化场景。

我想从浏览器下载一个excel文件,打开,保存,然后上传回网站。

我的问题是可以使用 python selenium 编辑下载的文件吗?

有人可以帮我解决这个问题吗?我可以使用的最佳方法是什么。

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。
  • 有可能,是的。 Apache POI 非常适合读取/写入 excel 文件。
  • 这里至少有三个不同的问题。请一次只关注一件事。
  • @pcalkins 感谢您的回复。你能解释一下我如何在我的python代码中包含它吗?
  • @BryanOakley 很抱歉造成混乱,但这是我的确切 1 个问题。有关于如何下载和如何使用 python 上传文件的答案。我将尝试准确地编辑我的问题“有没有一种方法可以使用 python selenium 自动编辑下载的文件”。

标签: python selenium download robotframework ui-automation


【解决方案1】:

您可以使用下面的代码再次复制和修改:

*** Settings ***
Documentation  File Upload Download in Robot Framework
Library  SeleniumLibrary
Library   OperatingSystem

*** Variables ***

*** Test Cases ***
Verify File Upload
    [documentation]  This test case verifies that a user can successfully upload a file
    [tags]  Regression
    Open Browser  https://the-internet.herokuapp.com/upload  Chrome
    Wait Until Element Is Visible  id:file-submit  timeout=5
    Choose File  id:file-upload  /Users/alapan/PycharmProjects/Robot-Framework/Resources/Upload/sunset.jpg
    Click Element  id:file-submit
    Element Text Should Be  tag:h3  File Uploaded!  timeout=5
    Element Text Should Be  id:uploaded-files  sunset.jpg  timeout=5
    Close Browser
   
Verify File Download
    [documentation]  This test case verifies that a user can successfully download a file
    [tags]  Regression
    ${chrome options}=  Evaluate  sys.modules['selenium.webdriver'].ChromeOptions()  sys, selenium.webdriver
    ${prefs}  Create Dictionary
    ...  download.default_directory=/Users/alapan/PycharmProjects/Robot-Framework/Resources/Download
    Call Method  ${chrome options}  add_experimental_option  prefs  ${prefs}
    Create Webdriver  Chrome  chrome_options=${chrome options}
    Goto  https://the-internet.herokuapp.com/download
    Click Link  css:[href="download/sunset.jpg"]
    Sleep  5s
    ${files}  List Files In Directory  /Users/alapan/PycharmProjects/Robot-Framework/Resources/Download
    Length Should Be  ${files}  1
    Close Browser

*** Keywords ***
Verify File Upload
    [documentation]  This test case verifies that a user can successfully upload a file
    [tags]  Regression
    Open Browser  https://the-internet.herokuapp.com/upload  Chrome
    Wait Until Element Is Visible  id:file-submit  timeout=5
    Choose File  id:file-upload  /Users/alapan/PycharmProjects/Robot-Framework/Resources/Upload/sunset.jpg
    Click Element  id:file-submit
    Element Text Should Be  tag:h3  File Uploaded!  timeout=5
    Element Text Should Be  id:uploaded-files  sunset.jpg  timeout=5
    Close Browser

接下来的代码解释可以看这个网站:https://testersdock.com/upload-download-robot-framework/

【讨论】:

  • 感谢您的回复。您能否分享您对如何使用机器人框架或 python 编辑该文件的见解?下载和编辑部分对我有用
【解决方案2】:

感谢大家的帮助。我能够通过使用 Python 附加文件功能来自动化我的场景。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 2021-02-09
    • 1970-01-01
    相关资源
    最近更新 更多