如何打开多个chrome窗口并强制它们始终处于活动状态?

628mspwn  于 2023-06-19  发布在  Go
关注(0)|答案(1)|浏览(339)

我试图使用selenium打开多个chrome窗口,但我总是面临一个问题,当我打开太多(如7个窗口)时,其中一些窗口将处于非活动状态,导致程序抛出NoSuchElementException。我不知道是否有一个问题与我的RAM,因为它似乎很好,也我运行在16GB RAM,所以我找不到一个原因的窗口去不活动。我想到了一个变通办法,但它是不是很充分的,这是打开他们在窗口模式,并保留所有他们。有没有其他的解决方案,比如在 chrome 或其他东西中设置?
编辑:这台电脑是专门为这项任务,所以没有其他进程是干扰程序

ulydmbyx

ulydmbyx1#

要使用Python和Selenium打开多个Chrome窗口并强制它们始终处于活动状态,您可以使用Selenium中的Options类来自定义Chrome浏览器设置。
下面是一个如何实现此目标的示例:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# Create Chrome options object
chrome_options = Options()

# Set the "--start-maximized" option to open the browser window maximized
chrome_options.add_argument("--start-maximized")

# Set the "--disable-popup-blocking" option to disable pop-up blocking
chrome_options.add_argument("--disable-popup-blocking")

# Set the "--disable-infobars" option to disable the infobars
chrome_options.add_argument("--disable-infobars")

# Set the "--disable-extensions" option to disable browser extensions
chrome_options.add_argument("--disable-extensions")

# Set the "--disable-notifications" option to disable notifications
chrome_options.add_argument("--disable-notifications")

# Create multiple WebDriver instances with the desired options
driver1 = webdriver.Chrome(options=chrome_options)
driver2 = webdriver.Chrome(options=chrome_options)

# Open URLs in the respective browser windows
driver1.get("https://www.example.com")
driver2.get("https://www.google.com")

在上面的代码中,Options类用于设置Chrome的各种命令行选项。您可以根据需要使用add_argument()方法添加更多选项。
在此示例中,选项设置为:

  • --start-maximized:打开浏览器窗口最大化。
  • --disable-popup-blocking:禁用弹出窗口阻止。
  • --disable-infobars:禁用信息栏。
  • --disable-extensions:禁用浏览器扩展。
  • --disable-notifications:禁用通知。

通过使用相同的选项创建多个webdriver.Chrome示例,您可以打开多个Chrome窗口。每个示例代表一个单独的浏览器窗口,您可以分别对它们执行操作。
请记住下载适当的ChromeDriver可执行文件,并在创建WebDriver示例时提供其路径。您可以从官方网站(https://sites.google.com/a/chromium.org/chromedriver/)下载ChromeDriver,并使用executable_path参数指定其位置。
注意:请记住,同时打开太多浏览器窗口可能会影响系统性能和资源使用。请确保负责任地使用此方法,并考虑系统的功能。

相关问题