Robotframework "Choose file" causes AttributeError: module 'base64' has no attribute 'encodestring' in docker

Robotframework "Choose file" causes AttributeError: module 'base64' has no attribute 'encodestring' in docker

我正在尝试 运行 在 docker 容器中进行测试,运行 在本地没有问题: 我想从 'correct' 目录

上传一个 correct.csv 文件
*** Keyword ***
Upload file
    [Arguments]    ${directory}    ${file}
    Choose File    ${choose_file_input}    ${EXECDIR}/Files/${directory}/${file}

** Test case ***
Upload
    Upload file    correct    correct.csv

但是当在 docker 中进行 运行ning 测试时,我得到一个 AttributeError 失败:模块 'base64' 没有属性 'encodestring'。是因为docker里面没有GUI吗?或者编码需要修复?或者最终我可以使用另一种解决方案来上传文件?

15:20:01.250    INFO    Sending /App/Files/correct/correct.csv to browser.  
15:20:01.251    DEBUG   POST http://192.168.1.29:4444/wd/hub/session/4b6d453b394adaaa51bb4149e9ba8678/elements {"using": "xpath", "value": "//div[@id=\"upload\"]//input"}  
15:20:01.252    DEBUG   Starting new HTTP connection (1): 192.168.1.29:4444 
15:20:01.305    DEBUG   http://192.168.1.29:4444 "POST /wd/hub/session/4b6d453b394adaaa51bb4149e9ba8678/elements HTTP/1.1" 200 90   
15:20:01.305    DEBUG   Finished Request    
15:20:01.618    FAIL    AttributeError: module 'base64' has no attribute 'encodestring' 
15:20:01.619    DEBUG   Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/SeleniumLibrary/__init__.py", line 490, in run_keyword
    return DynamicCore.run_keyword(self, name, args, kwargs)
  File "/usr/local/lib/python3.9/site-packages/robotlibcore.py", line 103, in run_keyword
    return self.keywords[name](*args, **(kwargs or {}))
  File "/usr/local/lib/python3.9/site-packages/SeleniumLibrary/keywords/formelement.py", line 224, in choose_file
    self.find_element(locator).send_keys(file_path)
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 475, in send_keys
    value = self._upload(local_file)
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 695, in _upload
    content = base64.encodestring(fp.getvalue())

根据回溯发现这个问题:

Selenium 3 is incompatible with Python 3.9

这是对问题的修复:DeprecationWarning of base64.encodestring()

他们不会支持移植此修复程序:

Thanks for the issue. We won't be releasing another version 3 as we're heading to finishing off Selenium 4. It is a drop in replacement to use Selenium 4.0.0.a5 so should work the same. There should not be any breaking changes.

  1. 因此您可以将 selenium 升级到 Selenium 4.0.0.a5 或
  2. 例如,
  3. 将 Python 降级到 3.7。我想在本地你不会 运行 3.9.

我们也 运行 解决了这个问题,但由于与其他库不兼容,无法返回到 Python 的旧版本。如果你发现自己在同一个地方,你可以像这样重新创建别名:

import base64
base64.encodestring = base64.encodebytes

无论你的切入点是什么。