python - Opening multiple links in default browser as tabs, why do I get multiple windows? -


I have just created a simple script that links to a hand-typed list opens in the default browser. It succeeds in opening pages, but often spreads to all pages by opening two or three windows. Web Browser New = 2 DRF Open_page (URL) Import: Webbrozer.Open (URL, new = new) DRF file_LAN (FNA): Open with FNA (FNA): For I, calculate in L (F) : Open return with i + 1 line_len = file_len ('social.txt') as the open ('social.txt') f: content1 = f.readlines () in the range I (line_len): Url = content1 [i ] .rstrip () open_page (url) print url

This is dependent on your browser As a documentation for:

If New 2, then a new browser page ("tab") is opened.

So, why can not it be possible?

There are at least two reasons, and perhaps more:

  • When you call webbrowser.open> second time, it is still open the first window Is not ready. Therefore, when he tries to create a new tab in the current window, then is is not a current window, so it creates a new tab. It may be some time if you are spamming as quickly as possible (As you are doing). Every browser works on every platform in this way, but some do it, it is especially likely in cases where "browser" program is actually just a script that excludes Mac OS X. Most Nix platforms Talks to the real browser program with the firefox script.
  • When you call webbrowser.open at 13th or a very long time, which is more than a certain limit on the maximum tab / window, it opens a new window. It is believed that lizard-based browsers have this feature but have been disabled in Firefox (e.g., to set you a limit about : preferences or prefs.js edit), it's not all in WebKit-based browsers Is not, and I have no information about the IE or Opera.

Again, these are just two possible reasons.


So, how do you fix it? Well, it depends on the problem.

First of all, some hackers are already working to avoid the first problem with some browsers. Specifically, if you are on a * nix platform, and your default browser is Firefox, but Python can not understand it (and so it is only using $ browser and / or < Code> Xdg-Open or similar), by default using firefox .

But beyond that, this is a classic race condition to solve the situation of any race, to find the right thing to synchronize on it. But there is no way to do this in this case. You are calling a function that prevents a series of events and does not react (for example, it executes a wrapper script which interacts with the actual browser program which is only a front-end The real real browser program that sends messages to the pipes ...) Docks says:

For non-UNIX platforms, or when a remote B on Unix If the browser is available, the control process will not wait for the user to end up with the browser, but allow the remote to keep your windows on display to the browser.

As always with the race, you can do paperwork with sleep s, but this is not a good solution. Regardless of how long you sleep during the first and second calls, it is not always guaranteed to be long-and it is usually certain to be very long.

If you dig through the source and follow UnixBrowser , Mozilla Browser , you can see that there are some hacks that You may be able to expand which can work in some cases. Or, you can go deeper under the cover and you can talk to a specific web browser using some more powerful SPEED API, which says, Comm or Apple Avent or on some pipes or on the command line. But that lacks, there is no real answer. (Well, you can use something like how many different browsers are known to run, but this is the only basic idea.)


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -