【问题标题】:BeautifulSoup import Failure when Running Robot Framework运行 Robot Framework 时 BeautifulSoup 导入失败
【发布时间】:2021-06-24 18:54:37
【问题描述】:

我在 MacOS 上运行机器人框架。我在 python 中编写了一个小的 beautifulsoup 函数,我可以在一个名为 BeautifulsoupFunction.py 的文件中直接成功地直接调用它(在函数内部和外部),如下所示:

from bs4 import BeautifulSoup
import requests

def get_image_urls(url):
    html = requests.get(url)
    source = BeautifulSoup(html.content,'html.parser')
    images = source.find(id='vi_main_img_fs_slider').find_all('img')
    image_urls = {}
    for img in images:
        image_urls.add(img['src'])
    return image_urls

调用它的 .robot 文件如下:

*** Settings ***
Library           SeleniumLibrary    timeout=10
Library    ./helper-scripts/BeautifulsoupFunction.py

*** Variables ***
${BROWSER}    chrome
${SITE}    somerandomurl.com
${SLEEP}    3

*** Test Cases ***
mytest
    Open Browser    ${SITE}    ${BROWSER}
    ${PRODUCT_TITLE}=    Get Text    xpath=//h1[@id='itemTitle']
    log to console    "This is the product: ${PRODUCT_TITLE}"
    &{PRODUCT_IMAGES}=    get_image_urls    ${SITE}

当通过 .robot 脚本调用时,python 文件由于某种原因无法导入 beautifulsoup,即使它是通过 pip 系统范围安装的:

[ ERROR ] Error in file '/Users/myusername/dev/robot-scripts/test.robot' on line 5: Importing library '/Users/myusername/dev/robot-scripts/helper-scripts/ebay_img_links.py' failed: ModuleNotFoundError: No module named 'bs4'
Traceback (most recent call last):
  File "/Users/myusername/dev/robot-scripts/helper-scripts/BeautifulsoupFunction.py", line 1, in <module>
    from bs4 import BeautifulSoup
PYTHONPATH:
  /usr/local/bin
  /usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python39.zip
  /usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9
  /usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload
  /usr/local/lib/python3.9/site-packages

有人知道为什么会发生这种情况吗?我是机器人框架的新手,对 Python 不熟悉,所以我希望它是初级的。

提前感谢您的帮助!

【问题讨论】:

  • 您的代码适用于我的机器人框架。当你在框架之外调用时,你是怎么做的? python ...python3 ...
  • 只是“python”,真的。在开箱即用的 Mac 上,我可以直接作为“robot nameofile.robot”运行,而在 Windows 上,我需要运行“python -m robot nameofile.robot”。两者都运行 python 版本 3.9.2,但奇怪的是,当我在 Windows 上运行时,它是失败的“请求”模块,而不是 BeautifulSoup。也许我需要旧版本?
  • 您在 Windows 上的 PATH 中没有 robot 可执行文件,仅此而已。您可以尝试使用旧版本,我有 3.7。

标签: python python-3.x beautifulsoup robotframework


【解决方案1】:

原来我的环境在我的 mac 上很乱,这导致了问题:不是版本控制或导入。从我的系统中擦除所有 python 痕迹,通过 brew 重新安装 python3,并拉回所有依赖项后,一切都开始正常工作了。

【讨论】:

    猜你喜欢
    • 2020-07-18
    • 2023-03-17
    • 2017-10-27
    • 2015-11-12
    • 1970-01-01
    • 2016-06-14
    • 2016-01-17
    • 2019-04-22
    • 2018-07-20
    相关资源
    最近更新 更多