1

I'm a python beginner and I want my code to open a local HTML page with parameters (e.g. /help/index.html#1). Right now I have the below code:

def openHelp(self, helpid):
    import subprocess
    import webbrowser
    import sys, os
    directory = os.getcwd()
    if sys.platform == 'darwin': # For macOS
        url = 'file://' + directory + '/help/index.html' + '#{}'.format(str(helpid))
        webbrowser.get().open(url)
    else:
        url = 'help/index.html' + '#{}'.format(str(helpid))
        webbrowser.open(url)

The code opens the webpage, however without the parameters (#helpid). Where did I make a mistake? Thanks in advance!

1 Answer 1

1

Your code looked fine to me. I tried it and it worked. You have to call it with openHelp("",1). The #helpid parameter was added correctly. Make sure it is a number.

Sign up to request clarification or add additional context in comments.

4 Comments

Hey thanks for the reply, I have called it with self.openHelp(1) but it does not work. Is it because I'm using Chrome with macOS?
May I ask the platform you're using?
I use Python 2.7.12 |Anaconda 4.1.1] and as browser I use: Mac Chrome Version 49.0.2623.112 (64-bit). My operating system is: OSX 10.7.5
Had the same problem, everything OK for local html-files with webbrowser.open("file://c:/.../my_file.html#hash") under linux/firefox, but windows/chrome strips any parameters/hashes and just opens the website in browser. Only workaround I found was to open the html-file in web webbrowser.open("https://.../my_file.html#hash") instead of local.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.