2

Can I get information about contents of the 'a' variable after using webbrowser.get? I can't get anything more than 'webbrowser.WindowsDefault object at 0x024643B0'. I'd like to know which browser is default and transfer this information to string

import webbrowser
a = webbrowser.get('windows-default')
print a
a.open('http://www.google.com')
JeremyK
  • 147
  • 1
  • 1
  • 10

1 Answers1

0

Here is a two step solution:

  1. Get the class name of the browser object by running: type(a).__class__
  2. Create a dictionary where you map a browser class name to a browser type name.

Details:

When you run a = webbrowser.get('windows-default'), you can get the class name of a by: browser_class = type(a).__class__.

Based on below picture, you can code the method highlighted in 2 as follows:

def which_browser(browser_class):
   return{
       'Mozilla': 'firefox',
       'Chrome' : 'google-chrome'           
   }.get(browser_class,'firefox')

This method returns the browser type name depending on the browser class name you get in browser_class above. Note that I set the method to return, by default mozilla but that is not necessarily for a simple test. I mean, you can simply run:

def which_browser(browser_class):
   return{
       'Mozilla': 'firefox',
       'Chrome' : 'google-chrome'           
   }

You can then call which_browser() method to get the browser type name:

browser_name = which_browser(browser_class)

browser_name is of type str as you want it.

You may need this information linked to above:

enter image description here

Full program:

I tested the following code according to what I have on my Linux machine where I have installed only 2 browsers:

import webbrowser

a = webbrowser.get('windows-default')
   
def which_browser(b):
   return{
       'Mozilla': 'firefox',
       'Chrome' : 'google-chrome'           
   }.get(b,'firefox')

print which_browser(type(a).__class__)

After running the program, I get this output: firefox.

Community
  • 1
  • 1
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
  • It doesn't work it just returns dictionary and I might be wrong but it looks like you give answer already in the program instead of asking for it. – JeremyK Apr 13 '16 at 15:01
  • What does not work? And no, I'm not injecting an answer to the program in advance. The fact you said the program returns a dictionary means you did not even try to run it, so good luck. – Billal Begueradj Apr 13 '16 at 16:22