【问题标题】:Python- Is it possible to open multiple Chrome web pages at once?Python-是否可以一次打开多个 Chrome 网页?
【发布时间】:2019-02-03 12:50:28
【问题描述】:

我正在编写一个 Python 脚本来一次打开多个网页,然后进行搜索。 Python 会一一打开它们,但速度非常慢。我在 Chrome 中打开了 12 个标签。 这是我用来在 Chrome 中的每个网站的新标签页中打开网站的代码。有什么建议? (使用 PyCharm,Python 3.5)

driver.get('https://www.website1.com')


    driver.execute_script("window.open('');")  # opens new tab
    driver.switch_to.window(driver.window_handles[1])
    driver.get('website2.com')


    driver.execute_script("window.open('');")  # opens new tab
    driver.switch_to.window(driver.window_handles[2])
    driver.get('website3.com')

【问题讨论】:

  • 你可以使用pytest对每个网页进行测试,然后使用xdist插件在线程中运行测试。

标签: python python-3.x automation selenium-chromedriver


【解决方案1】:

我是新手,但我希望这对您的问题有所帮助。这将打开一个窗口,然后在该窗口中打开选项卡。

import webbrowser

url = 'http://website1.com'
url_1 = 'http://website2.com'

chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

webbrowser.get(chrome_path).open(url)
webbrowser.get(chrome_path).open(url_1)

由于我无法让open_new() 工作,如何让它打开一个新浏览器。

import webbrowser
import os

url = 'http://python.org/'

chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

os.startfile('C:/Program Files (x86)/Google/Chrome/Application/chrome.exe', "open")

webbrowser.get(chrome_path).open_new(url)
webbrowser.get(chrome_path).open(url + '/doc')

【讨论】:

  • 很好的答案,简短而甜蜜,难以击败!点赞!
  • 好的,这样可以更快地加载网页,但是,它会在我打开的任何 chrome 窗口中打开选项卡。有没有办法指定一个新窗口,然后在该窗口中指定选项卡?一旦它们打开,我将向不同的句柄发送搜索词,如果它在现有的 chrome 窗口中打开,我不确定如何指定句柄。
  • 根据您在 chrome 中的设置,webbrowser.get(chrome_path).open_new(url) 将打开一个新窗口。 .open_new_tab(url) 将打开一个新标签。那对我不起作用,所以我这样做了,它起作用了,但是它添加了一个空白选项卡,我不确定如何避免。我会将代码添加到我的原始答案中。希望这会有所帮助。
【解决方案2】:

我最终做的是打开一个网页使用- driver.get('https://www.website1.com')

然后模拟击键以在 chrome 中打开一个新选项卡,输入网址并按 Enter。 这似乎是让页面加载的最快方法,而不必等待它们加载一个然后继续下一个。

【讨论】:

    【解决方案3】:

    现在,可以简单地做(使用默认浏览器):

    import webbrowser
    url1 = "www.google.com"
    url2 = "www.facebook.com"
    webbrowser.open_new(url1)
    webbrowser.open_new(url2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 2015-05-09
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      相关资源
      最近更新 更多