如何使用机器人框架和硒读取 Web 控制台输出? [英] How to read the web console output with robot framework and selenium?
问题描述
我正在尝试读取网页的控制台输出,尤其是我需要使用 RF 和 Selenium 进行 POST-GET-PUT ajax 调用.我在网上找到了一些帮助,但我似乎无法让它工作.我拥有的python脚本是:
I am trying to read the console output of a webpage, especially I need the POST-GET-PUT ajax calls, with RF and Selenium. I have found some help online but I cannot seem to make it work. the python script I have is:
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 调用了这个脚本.所以我需要向这个函数传递我采取的行动之后的页面.要做到这一点,我会这样做:
I call this script from RF, after having done some operations on the webpage. So I need to pass to this function the page exactly how it is after the actions I took. To do that I do:
${seleniumlib}= Get Library Instance SeleniumLibrary
Log ${seleniumlib._drivers.active_drivers}[0]
${message} = Get Logs2 ${seleniumlib._drivers.active_drivers}[0]
我得到的结果是空消息,但我知道控制台不是空的.你能帮我吗?谢谢.
I get as a result and empty message, but I know console is not empty. Can you help? Thanks.
推荐答案
这是一个完全使用 Robot Framework 的解决方案,没有额外的用户库.逻辑是一样的.
Here is a solution using entirely Robot Framework, no additional user library. The logic is the same.
- 设置正确的浏览器功能以启用日志记录.
- 然后使用
Get Library Instance
关键字来检索 webdriver 实例. - 调用 webdriver 实例上的
get_log('browser')
.
- Set the correct browser capabilities to enable logging.
- Then use the
Get Library Instance
keyword to retrieve the webdriver instance. - Call the
get_log('browser')
on the webdriver instance.
*** 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}
这篇关于如何使用机器人框架和硒读取 Web 控制台输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!