简体   繁体   中英

How can I use the python webbrowser module to open a link in the background?

I'm trying to open a link in the background using the webbrowser module. Ive looked online and I cant seem to find anything, I want to use webbrowser over subprocess because webbrowser will automatically use the default browser. I have this code to open a url normally but thats as far as I have been able to get.

import webbrowser

background_url = 'google.com'

webbrowser.open_new(background_url)

If you don't want the user to see or interact with the browser you should use what's known as a headless browser. webbrowser is designed specifically for users to see. Here's what the docs say: "The webbrowser module provides a high-level interface to allow displaying web-based documents to users." (emphasis added).

A headless browser is run without a GUI so it is invisible to the user and not impacted by user input. They will still load the website's information and can interact with the website while taking up less time and resources. Here's a fairly exhaustive list of headless browsers you can use. Note that not all are compatible with Python.

As Codeman mentioned in the comments Selenium is an option. Selenium is very well documented and has a large community following so it's easy to find helpful articles or tutorials. It's a multi-driver so it can run different types of browsers (firefox, chrome, etc.), both headless and with a GUI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM