Opening Several Tabs In Firefox Using Python
Solution 1:
The way webbrowser
works in the background consists of creating subprocess.Popen
objects. This has two underlying effects that map directly with the reported results:
If Firefox is open, as
webbrowser
detects that Firefox is already running, it sends the correct argument toPopen
, which results in every URL opening in the current Firefox window.If no Firefox process exists, then what happens is that several concurrent requests are being made to Firefox to access the passed URLs. As no window exists, there isn't a way to create tabs and the last option Firefox has is to present every link in a separate window.
I managed to solve this problem by simply joining all URL's while calling Firefox. This has certain limitations though (referent to the character limit of a command string) but it is a very simple and efficient solution:
import subprocess
firefox_path = "C:/Program Files/Mozilla Firefox/firefox.exe"
cmdline = [firefox_path]
withopen('url_list.txt', 'r') as url_file:
for url in url_file:
cmdline.append(url)
subprocess.Popen(cmdline)
Solution 2:
A way to do it without hard-coding the browser path is to open the first one, wait a few seconds, and then open the rest:
import webbrowser, time
withopen('url_list.txt', 'r') as url_file:
urls = [line.strip() for line in url_file]
webbrowser.open(urls[0])
time.sleep(4)
for url in urls[1:]:
webbrowser.open_new_tab(url)
Solution 3:
driver = webdriver.Ie("C:\\geckodriver.exe")
binary = FirefoxBinary('usr/bin/firefox') # Optional
driver = webdriver.Firefox(firefox_binary=binary) # Optionaldriver.get("http://www.google.com")
driver.maximize_window()
Post a Comment for "Opening Several Tabs In Firefox Using Python"