【问题标题】:webbrowser not opening new windows网络浏览器不打开新窗口
【发布时间】:2016-11-29 04:50:16
【问题描述】:

我刚得到一份远程工作的新工作,我必须打开一堆页面并登录它们来开始我的一天。我很想自动化这个过程,因为它可能有点乏味。我想单独留下我的个人浏览窗口并打开一个包含我需要的所有页面的新窗口。这是我正在尝试做的事情的要点:

import webbrowser
first = True
chromePath = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
URLS = ("first page", "second page", "third page")
for url in URLS:
    if first:
        webbrowser.get(chromePath).open(url)
        first = False
    else:
        webbrowser.open(url, new=2)

由于某种原因,这段代码只是在我当前的浏览器中打开新标签,这与我希望它做的基本上相反。怎么回事?

【问题讨论】:

  • 你想在一个单独的窗口中打开每个 url 还是启动一个包含所有选项卡的新窗口?
  • 包含所有标签的新窗口。
  • 我怀疑您已经设置了 chrome,以便为新页面创建标签,而不是打开新浏览器。也许使用其他浏览器 (firefox) 进行自动负载设置?

标签: python python-webbrowser


【解决方案1】:

我没有安装 Chrome,但似乎有多个问题:

  1. 根据文档,webbrowser.get 需要浏览器的名称,而不是路径。
  2. 你应该保存webbrowser.get()的返回值并用它来打开剩余的url。

import webbrowser
URLS = ("first page", "second page", "third page")
browser= webbrowser.get('chrome')
first= True
for url in URLS:
    if first:
        browser.open_new(url)
        first = False
    else:
        browser.open_new_tab(url)

【讨论】:

  • 感谢您的意图,但这不是问题的解决方案。您可以连接/拆分方法,但结果将与 webbrowser.get(chromepPath).open(url) 相同。问候
【解决方案2】:

可能为时已晚,但可能会帮助其他人。

根据文档,您应该尝试使用 new=1

webbrowser.open(url, new=0, autoraise=True)

使用默认浏览器显示网址。如果new为0,则打开url 如果可能,在同一个浏览器窗口中。如果 new 为 1,则为新浏览器 如果可能,打开窗口。如果 new 是 2,一个新的浏览器页面(“tab”) 尽可能打开。如果 autoraise 为 True,则在以下情况下会升起窗口 可能(请注意,在许多窗口管理器下,这会发生 无论此变量的设置如何)。

文档链接:Webbrowser docs

【讨论】:

    【解决方案3】:

    我遇到了类似的问题。我刚刚使用 os.system() 打开了一个新的 chrome 实例。我还使用以下方法将 chrome.exe 注册为新浏览器:

    #register the browser
    chrome_path = "C:\\Users\\cj9250\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe"
    webbrowser.register('chrome', None,webbrowser.BackgroundBrowser(chrome_path),1)
    

    other posts 中有关于使用 .get 调用 chrome 路径的指导。尽管尝试了这种方法和许多其他不同的方法,我还是无法让 chrome 打开一个新窗口和会话。

    最后我来到 os.system 打开一个新的 chrome 实例。因此,对于您的代码,它看起来像这样:

    import webbrowser
    import os #use for new instance of chrome
    
    #urls I want to open in array
    URLS = (
            "first page", 
            "second page", 
            "third page"
            )
    
    #register the browser
    chrome_path = "YOUR PATH TO CHROME\\chrome.exe" #note the double \ syntax
    webbrowser.register('chrome', None,webbrowser.BackgroundBrowser(chrome_path),1)
    
    #open new instance of chrome
    os.system(r'YOUR PATH TO CHROME\chrome.exe')
    
    #open each url
    for url in URLS:
        webbrowser.get('chrome').open(url)
    

    看看你想要做什么,我认为 Selenium 会更适合。我也每天在工作中使用 Web 应用程序,并且花时间浏览了一些 Selenium 教程。自动化要简单得多。

    【讨论】:

    • 对此有两点说明。 首先:您需要在os.system 调用中再次使用引号中的浏览器路径。那是因为路径可能包含空格。例如os.system(r'"C:\Program Files\...\chrome.exe"')第二: 调用webbrowser.open_new_tab("<url>") 即可,无需先注册浏览器,之后调用os.system(...)
    【解决方案4】:

    解决方案要简单得多。假设您有 chrome.exe 的标准路径 ubication(并且您的操作系统是 windows),这就是解决方案(即使您之前已经打开了一个新的 chrome 窗口,它也会打开一个新的 chrome 窗口)。 subprocess 而不是 os.system 是强制性的,否则如果已经打开了一个新的 chrome 窗口,它就不会打开一个新的 chrome 窗口。问候

    import subprocess
    urL         ='https://www.google.com'
    chrome_path ="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
    child       = subprocess.Popen(chrome_path, shell=True)
    

    【讨论】:

      【解决方案5】:

      这是在 Windows 机器上完成的。

      我基本上面临同样的问题;无法打开新的浏览窗口,只能打开新标签。

      这是一种解决方法,它允许我使用初始 URL 打开一个浏览窗口(在 Chrome 中),然后在该窗口中将后续 URL 作为选项卡打开。

      import webbrowser
      import subprocess
      
      # Set this appropriately depending on your situation
      chrome_path = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
      first_url = 'https://www.google.com/'
      
      # This should open a NEW WINDOW with the URL specified above
      command = f"cmd /C \"{chrome_path}\" {first_url} --new-window"
      
      # Alternative way that achieves the same result
      # command = f"cmd /C start chrome {first_url} --new-window"
      
      subprocess.Popen(command)
      
      new_tab_urls = [
          'https://www.python.org/',
          'https://hackernoon.com/30-jokes-only-programmers-will-get-a901e1cea549'
      ]
      
      for url in new_tab_urls:
          webbrowser.open_new_tab(url)
      

      【讨论】:

        猜你喜欢
        • 2013-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-09
        • 2020-05-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多