4

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

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)

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

4

5 回答 5

2

我遇到了类似的问题。我刚刚使用 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)

其他帖子中有关于使用 .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 教程。自动化要简单得多。

于 2018-07-05T18:22:08.773 回答
2

我没有安装 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)
于 2016-07-25T18:25:22.507 回答
2

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

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

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

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

文档链接:Webbrowser 文档

于 2017-04-25T17:44:48.980 回答
2

解决方案要简单得多。假设您有 chrome.exe 的标准路径 ubication(并且您的操作系统是 windows),这就是解决方案(即使您之前已经打开了一个新的 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)
于 2020-05-12T13:22:51.720 回答
0

这是在 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)
于 2020-12-26T11:50:11.317 回答