Access browser logs in Selenium? - python

Is there a way to access logs on the browser made by the sever in Selenium? For example, if the site executed a console.log("Test."), can a Selenium test case access that log? Any help would be appreciated! (Preferably in Python).
Thanks!
~Carpetfizz

You could inject javascript and override the log function (as reading the log is not permitted from JS).
See http://tobyho.com/2012/07/27/taking-over-console-log/ for an example and there are many SO questions on this topic.
The problem with this is that it still can't get the logs which happened prior to injection.
It gets easier if you override this in the test deployment directly.

Related

[Python][Web Scraping] is there a way to prevent cache clearing when executing my script and the browser opens up?

I m a newbie so I will try to explain myself in a way it makes sense.
I produced my first ever python script to scrape data from a web page I use regularly at work. It just prints out couple of values in the console that previously I had to consult manually.
My problem is that every time I execute the script and the browser opens up, it seems the cache is cleared and I have to log in into that work webpage using my personal credentials and do the 2 factor authentication with my phone.
I m wondering wether there is a way to keep the cache for that browser (if I previously already logged into the web page) so I don´t need to go through authentication when I launch my script.
I m using selenium webdriver and chrome, and the option I have configured are these (in screenshot below). Is there perhaps another option I could add to keep cache?
Current options for browser
I tried to find info in the web but so far nothing.Many sites offer a guide on how to perform login by adding lines of code with the username and the password, but I would like to avoid that option as I still would need to use my phone for the 2 factor authentication, and also because this script could be used by some other colleagues in the future.
Thanks a lot for any tip or info :)
After days browsing everywhere, I found this post:
How to save and load cookies using Python + Selenium WebDriver
the second answer is actually the one that saved my life; I just had to add this to my series of options:
chrome_options.add_argument("user-data-dir=selenium")
see the provided link for the complete explanation of the options and imports to use.
Adding that option, I run the script for the first time and I still have to do the login manually and undergo authentication. But when I run it for the second time I don´t need any manual input; the data is scraped from the web, the result is returned and no need any manual action from me.
If anybody is interested in the topic please ping me.
Thanks!

Can I declare 2 desired_cap in 1 script? One for hybrid app and one for the Chrome app?

I have a hybrid app with redirects on the chrome browser.
In order to make appium recognize both apps, is there a possibility to declare a double desired_cap?
I want to make the script to click on one of the hybrid app button, then after the redirect to chrome is done, a verification of the chrome page should be done (so we can know that the redirect was done correctly) and then we have the driver.back() which will lead us back to the hybrid testing app.
I know is wrong what I've wrote, but can't find the right way.. got the error that the __second__ is not defined.
My example:
Can anyone help me with that? Thanks in advance.
You don't need to change the Desired Capabilities and also it is not possible during the session.
You must switch between the context of session (WEB_VIEW or NATIVE):
When the chrome opened do this:
driver.switch_to.context['WEB_VIEW_chrome]
Check your verifications and then you can change the context to the Native to continue on your android application:
driver.switch_to.context['NATIVE_APP]
Here is my full answer on this:
https://stackoverflow.com/a/62284044/7302505

Read Internet Explorer console output with Selenium

I'm working on testing automation in Internet Explorer 11 with Selenium and I'm looking to read any console output for errors. However, any research I pulled up lead to a 2 year old response saying that the IE driver doesn't support reading logs of any kind (see here). Has there been any update to this issue? If not, is there any workaround to reading JS errors in IE with Selenium?
No, there is no change to the log API not being implemented in the IE driver. One reason for this is the arrival of the W3C WebDriver Specification, which does not define any logging end points. Moreover, even if the driver were to implement the logging API, getting the console log in IE would still be impossible, since Internet Explorer does not provide any programmatic access to its debugging tools.
One approach to capturing JavaScript errors in IE is to set window.onerror and read any errors that occur there. Of course, this will not retrieve any JavaScript errors that occur during onLoad, or before the error handler is attached to the onError event. To accomplish that, another approach I've seen used is to use a proxy to inject the event handler script into the page before it gets to the browser. This blog post shows an example of how to do that. Even though the example is written in C#, the same technique can be applied in any of the other language bindings.

Django print in prod' server

I've gotten use to using print in my python code to show contents of variable and checking the shell output.
But i have now migrated all my work onto a online server. Pythonanywhere
I don't have the foggiest idea how to do the same now?
Can someone point me in the right direction?
Print to web console? To a file? Or even to the shell session?
Thanks
On production server your print statements will output log to your webserver log files
In case of pythonanywhere there are three log files
Access log:yourusername.pythonanywhere.com.access.log
Error log:yourusername.pythonanywhere.com.error.log
Server log:yourusername.pythonanywhere.com.server.log
those logs are accessible in your web tab page.
The logs you are looking for will be in server.log
As mentioned in Serjik's answer you can see the output of the console via the server log link on PythonAnywhere.
However the much better way to approach this is to use the Python logging module.. Using this module will solve many of these problems for you and solve many issues you may not have thought about (like thread safety). This lets you do things like filter log messages by severity and a whole bunch of other things.
To get started with that I would recommend having a look at the basic logging tutorial.

python web script send job to printer

Is it possible for my python web app to provide an option the for user to automatically send jobs to the locally connected printer? Or will the user always have to use the browser to manually print out everything.
If your Python webapp is running inside a browser on the client machine, I don't see any other way than manually for the user.
Some workarounds you might want to investigate:
if you web app is installed on the client machine, you will be able to connect directly to the printer, as you have access to the underlying OS system.
you could potentially create a plugin that can be installed on the browser that does this for him, but I have no clue as how this works technically.
what is it that you want to print ? You could generate a pdf that contains everything that the user needs to print, in one go ?
You can serve to the user's browser a webpage that includes the necessary Javascript code to perform the printing if the user clicks to request it, as shown for example here (a pretty dated article, but the key idea of using Javascript to call window.print has not changed, and the article has some useful suggestions, e.g. on making a printer-friendly page; you can locate lots of other articles mentioning window.print with a web search, if you wish).
Calling window.print (from the Javascript part of the page that your Python server-side code will serve) will actually (in all browsers/OSs I know) bring up a print dialog, so the user gets system-appropriate options (picking a printer if he has several, maybe saving as PDF instead of doing an actual print if his system supports that, etc, etc).

Categories

Resources