【问题标题】:How to read the web console output with robot framework and selenium?如何使用机器人框架和硒读取 Web 控制台输出?
【发布时间】:2021-02-17 16:09:32
【问题描述】:

我正在尝试读取网页的控制台输出,尤其是我需要带有 RF 和 Selenium 的 POST-GET-PUT ajax 调用。我在网上找到了一些帮助,但我似乎无法让它发挥作用。我拥有的python脚本是:

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

def get_logs2(driver):
# enable browser logging
    #d = DesiredCapabilities.CHROME
    #d['goog:loggingPrefs'] = { 'browser':'ALL' }
    #driver = webdriver.Chrome(desired_capabilities=d)

    # load the desired webpage
    #driver.get(driver.current_url)
    a = driver.get_log('browser')

    # print messages
    for entry in driver.get_log('browser'):
        print(entry)
    print("finished")
    return a

在对网页做了一些操作之后,我从 RF 调用了这个脚本。所以我需要在我采取的行动之后将页面准确地传递给这个函数。为此,我会这样做:

    ${seleniumlib}=    Get Library Instance    SeleniumLibrary
    Log    ${seleniumlib._drivers.active_drivers}[0]
    ${message} =    Get Logs2   ${seleniumlib._drivers.active_drivers}[0]

结果我得到一个空消息,但我知道控制台不是空的。你能帮我吗?谢谢。

【问题讨论】:

  • 这能回答你的问题吗? Robot Frameworkget background call with Robot Framework/selenium你的代码看起来很可疑,这不是问题,但它只会让你的问题重复。
  • 是的,实际上我让我的朋友 Sven 问了这个问题,因为我没有帐户。我认为结束这个问题并跟进这个问题是件好事。我尝试使用 robots.api 记录器,但由于字符串为空,它不会打印任何内容。
  • 我已经更新了另一个问题的答案。另一个问题不能删除,因为我已经回答过了,这个问题没有答案,你可以删除这个问题。
  • 太好了,谢谢,解决方案有效,但现在我遇到了一个新问题。控制台仅在开发人员工具设置中选中“Log XMLHttpRequests”时记录 ajax 调用。默认情况下不是,所以我在日志中看不到调用。您知道如何在 RF 的“设置”中单击该框吗?谢谢。

标签: selenium robotframework


【解决方案1】:

这是一个完全使用机器人框架的解决方案,没有额外的用户库。 逻辑是一样的。

  1. 设置正确的浏览器功能以启用日志记录。
  2. 然后使用 Get Library Instance 关键字检索 webdriver 实例。
  3. 在 webdriver 实例上调用 get_log('browser')

*** Settings ***
Library    SeleniumLibrary

*** Variables ***
&{browser logging capability}    browser=ALL
&{capabilities}    browserName=chrome    version=${EMPTY}    platform=ANY    goog:loggingPrefs=${browser logging capability}

*** Test Cases ***
Browser Log Cases
    Open Browser    https://stackoverflow.com    Chrome    desired_capabilities=${capabilities}
    ${log entries}=    Get Browser Console Log Entries    
    Log    ${log entries}
    [Teardown]    Close All Browsers
    
    
*** Keywords ***
Get Browser Console Log Entries
    ${selenium}=    Get Library Instance    SeleniumLibrary
    ${webdriver}=    Set Variable     ${selenium._drivers.active_drivers}[0]
    ${log entries}=    Evaluate    $webdriver.get_log('browser')
    [Return]    ${log entries}

【讨论】:

    猜你喜欢
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 2017-08-12
    • 2021-03-19
    • 2021-01-17
    • 1970-01-01
    相关资源
    最近更新 更多