I am using Selenium with ChromeDriver to get the performance log from Chrome.
caps = DesiredCapabilities.CHROME
caps['loggingPrefs'] = {'performance': 'ALL'}
driver = webdriver.Chrome(desired_capabilities=caps)
driver.get("some.website")
time.sleep(5)
for entry in driver.get_log('performance'):
print(entry)
Inspecting these messages, I found a timestamp field with weird value (779922.902049). So my question is what is the format of this timestamp field?
{'message': '{"message":{"method":"Network.responseReceived","params":{"frameId":"29983.1","loaderId":"29983.1","requestId":"29983.1","response":{"connectionId":0,"connectionReused":false,"encodedDataLength":0,"fromDiskCache":fals
e,"fromServiceWorker":false,"headers":{"Access-Control-Allow-Origin":"*","Content-Type":"text/plain;charset=US-ASCII"},"mimeType":"text/plain","protocol":"data","securityState":"unknown","status":200,"statusText":"OK","url":"data:
,"},"timestamp":779922.902049,"type":"Document"}},"webview":"e6d532fe-f007-4397-bf28-4c4a26c79e4d"}', 'level': 'INFO', 'timestamp': 1523607170083}
{'message': '{"message":{"method":"Network.loadingFinished","params":{"encodedDataLength":0,"requestId":"29983.1","timestamp":779922.902067}},"webview":"e6d532fe-f007-4397-bf28-4c4a26c79e4d"}', 'level': 'INFO', 'timestamp': 152360
7170083}
Note that there is another timestamp field in these above message which is milliseconds since epoch. But it is not the field I am asking.
I found that it is number of seconds from last system boot time (i.e. system uptime). I cannot find any document which describe the format of this timestamp though.
Related
I'm currently using selenium to get console logs from a chrome browser. My setup code is this and it works well for getting console logs
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# enable browser logging
d = DesiredCapabilities.CHROME
d['goog:loggingPrefs'] = { 'browser':'ALL' }
driver = webdriver.Chrome(desired_capabilities=d)
# load the desired webpage
driver.get(http://192.168.5.5)
# print messages
for entry in driver.get_log('browser'):
print(entry)
The problem is that entry is a dictionary that contains a string of Objects. I can not access what is inside this Object. In a real browser I can expand on these objects and see further information which is usually in the form of a dictionary. How do I get access to these console log object's in python like I would on a browser? It does not have to be through selenium if it is not possible in selenium.
Edit:
Example of the output from entry:
{'level': 'INFO', 'message': 'http://192.168.5.5/app.e52b3dcc.bundle.js 58:31633 "[END REQUEST]: Response:" Object Object', 'source': 'console-api', 'timestamp': 1663609942539}
Example of the object expanded in the browser (The copy and paste doesn't look as good here but you get the idea):
```END REQUEST]: Response:
Object
data1
:
"SOMEPIECEOFDATA"
response
:
{moreData: {…}, status: 1, type: 'response'}
[[Prototype]]
:
Object
Your code is essentially valid. Take this page with your question for example:
caps['goog:loggingPrefs'] = {'browser':'ALL' }
[...]
url = 'https://stackoverflow.com/questions/73778005/python-browser-console-log-objects'
driver.get(url)
logs = driver.get_log('browser')
for item in logs:
print(item)
Result in terminal:
{'level': 'WARNING', 'message': "security - Error with Feature-Policy header: Unrecognized feature: 'speaker'.", 'source': 'security', 'timestamp': 1663614945300}
{'level': 'WARNING', 'message': 'https://pagead2.googlesyndication.com/gpt/pubads_impl_2022091301.js 9:37758 "The following functions are deprecated: googletag.pubads().setTagForChildDirectedTreatment(), googletag.pubads().clearTagForChildDirectedTreatment(), googletag.pubads().setRequestNonPersonalizedAds(), and googletag.pubads().setTagForUnderAgeOfConsent(). Please use googletag.pubads().setPrivacySettings() instead."', 'source': 'console-api', 'timestamp': 1663614947286}
Maybe you need to investigate the logging process itself?
I have a problem with reading the date value from Pepper robot log. I use Python to fetch the log messages from a remote robot. See an example code:
def onMessage(mess):
print mess # mess is a dictionary with all known LogMessage information.
def main():
app = qi.Application(url="tcp://10.0.11.252:9559")
app.start()
logmanager = app.session.service("LogManager")
listener = logmanager.getListener()
listener.onLogMessage.connect(onMessage)
app.run()
if __name__ == "__main__":
main()
Тhis is how one log message looks like:
{
'category': 'ALMemory',
'level': 5,
'source': ':notify:0',
'location': '7b5400e2-18b1-48e4-1127-g4e6544d0621b:3107',
'date': 11112334305291,
'message': 'notifying module: _WholeBodyLooker for datachange for key: ALTracker/ObjectLookAt',
'id': 5599547,
'systemDate': 1533208857670649344,
}
The problem is that I don't know the date value meaning. I didn't find any documentation for this value. When I try to covert 11112334305291 to date the result is not meaningful: Sunday, February 19, 2322 11:31:45.291 PM.
Does anyone have any idea what this might mean?
Most likely nanoseconds since the robot has been on (so in your case, about three hours)- see the qi clock API in the documentation.
I have a field datetime. This field should have by default the datetime of "now", the current time.
However, the default date is the time of the lastest restart.
Please find below my code:
'date_action': fields.datetime('Date current action', required=False, readonly=False, select=True),
_defaults = {
'date_action': fields.datetime.now(),
You are setting the default value of date_action as the value returned by fields.datetime.now(), that is executed when odoo server is started.
You should set the default value as the call to the method:
'date_action': fields.datetime.now,
try to use lambda
For example in Odoo 8 :
date_action = fields.Datetime(string="Date current action", default=lambda *a: datetime.now())
I'm using Selenium to run tests in Chrome via the Python API bindings, and I'm having trouble figuring out how to configure Chrome to make the console.log output from the loaded test available. I see that there are get_log() and log_types() methods on the WebDriver object, and I've seen Get chrome's console log which shows how to do things in Java. But I don't see an equivalent of Java's LoggingPreferences type in the Python API. Is there some way to accomplish what I need?
Ok, finally figured it out:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# enable browser logging
d = DesiredCapabilities.CHROME
d['loggingPrefs'] = { 'browser':'ALL' }
driver = webdriver.Chrome(desired_capabilities=d)
# load the desired webpage
driver.get('http://foo.com')
# print messages
for entry in driver.get_log('browser'):
print(entry)
Entries whose source field equals 'console-api' correspond to console messages, and the message itself is stored in the message field.
Starting from chromedriver, 75.0.3770.8, you have to use goog:loggingPrefs instead of loggingPrefs:
d['goog:loggingPrefs'] = { 'browser':'ALL' }
To complete the answer: starting from chromedriver 75.0.3770.8, you have to use goog:loggingPrefs instead of loggingPrefs.
See Chromedriver changelog: http://chromedriver.chromium.org/downloads or this bug: https://bugs.chromium.org/p/chromedriver/issues/detail?id=2976
if you are using the python logging module (and you should be)... here is a way to add the selenium browser logs to the python logging system..
the get_browser_log_entries() function grabs the logs from eth provded driver, emits them to the python logging module as chrome. (ie chrome.console-api, chrome.network etc..) using the timestamp from the browser.(in case there is a delay before you call get_log)
it could probably do with some better exception handling (like if logging is not turned on ) etc.. but it works most of the time..
hop
import logging
from selenium import webdriver
def get_browser_log_entries(driver):
"""get log entreies from selenium and add to python logger before returning"""
loglevels = { 'NOTSET':0 , 'DEBUG':10 ,'INFO': 20 , 'WARNING':30, 'ERROR':40, 'SEVERE':40, 'CRITICAL':50}
#initialise a logger
browserlog = logging.getLogger("chrome")
#get browser logs
slurped_logs = driver.get_log('browser')
for entry in slurped_logs:
#convert broswer log to python log format
rec = browserlog.makeRecord("%s.%s"%(browserlog.name,entry['source']),loglevels.get(entry['level']),'.',0,entry['message'],None,None)
rec.created = entry['timestamp'] /1000 # log using original timestamp.. us -> ms
try:
#add browser log to python log
browserlog.handle(rec)
except:
print(entry)
#and return logs incase you want them
return slurped_logs
def demo():
caps = webdriver.DesiredCapabilities.CHROME.copy()
caps['goog:loggingPrefs'] = { 'browser':'ALL' }
driver = webdriver.Chrome(desired_capabilities=caps )
driver.get("http://localhost")
consolemsgs = get_browser_log_entries(driver)
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)7s:%(message)s')
logging.info("start")
demo()
logging.info("end")
Note that calling driver.get_log('browser') will cause the next call to return nothing until more logs are written to the console.
I would suggest saving the logs to a variable first. For example below logs_2 will equal [].
If you need something in the console to test you can use:
self.driver.execute_script("""
function myFunction() {
console.log("Window loaded")
}
if(window.attachEvent) {
window.attachEvent('onload', myFunction());
} else {
if(window.onload) {
var curronload = window.onload;
var newonload = function(evt) {
curronload(evt);
myFunction(evt);
};
window.onload = newonload;
} else {
window.onload = myFunction();
}
}
""")
logs_1 = driver.get_log('browser')
print("A::", logs_1 )
logs_2 = driver.get_log('browser')
print("B::", logs_2 )
for entry in logs_1:
print("Aa::",entry)
for entry in logs_2:
print("Bb::",entry)
See the answer from msridhar for what should go above my example code.
I am trying to migrate some existing blog entries into our confluence wiki using XML-RPC with Python. It is currently working with such things as title, content, space etc but will not work for created date.
This is what was currently attempted
import xmlrpclib
proxy=xmlrpclib.ServerProxy('<my_confluence>/rpc/xmlrpc')
token=proxy.confluence1.login('username', 'password')
page = {
'title':'myTitle',
'content':'My Content',
'space':'myspace',
'created':sometime
}
proxy.confluence1.storePage(token, page)
sometime is the date I want to set to a time in the past. I have tried using Date objects, various string formats and even the date object returned by a previous save, but no luck.
If you would try to store the existing content as actual blog entries in Confluence, then you could use the "publishDate" parameter:
import xmlrpclib
import datetime
proxy=xmlrpclib.ServerProxy('<my_confluence>/rpc/xmlrpc')
token=proxy.confluence1.login('username', 'password')
blogpost = {
'title' : 'myTitle',
'content' : 'My Content',
'space' : 'myspace',
'publishDate' : datetime.datetime(2001, 11, 21, 16, 30)
}
proxy.confluence1.storeBlogEntry(token, blogpost)
The XML-API for pages ignores the "created" parameter.
You can use strptime because type will not match directly. Hope this works.
new_sometime = datetime.strptime(sometime, '%Y-%m-%d')
page = {
'title':'myTitle',
'content':'My Content',
'space':'myspace',
'created':new_sometime
}