【问题标题】:Python selenium keep browser openPython selenium 保持浏览器打开
【发布时间】:2018-08-15 19:38:08
【问题描述】:

出于营销原因,我正在使用 selenium 打开一些浏览器窗口。我只需打开我的营销资源,通过 selenium 登录并开始工作。

问题是,代码执行后 selenium 会关闭窗口。

到目前为止,所有解决方案都没有太大帮助。

我有 13 个浏览器窗口 atm.,如下所示:

def open_instagram():    
    try:
        # Connect
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument("--incognito")
        browser = webdriver.Chrome('drivers/chromedriver.exe', chrome_options=chrome_options)
        browser.set_window_size(1800, 900)
        browser.get("https://www.instagram.com/accounts/login/?hl=de")
        browser.find_element(By.NAME, 'username').send_keys('MYEMAIL', Keys.TAB, 'MYPW', Keys.ENTER)
    except Exception as e:
        print (e, 'Instagram')

open_instagram()

我找到的最接近的解决方案是在我的脚本末尾添加这个,但不知何故它只会保持 5 个窗口打开,而不是关闭 5 个窗口并打开接下来的 5 个新窗口:

while True:
    pass

我只想让 selenium 保持所有浏览器窗口打开,直到我手动关闭它们。

【问题讨论】:

  • 如果您需要它来手工编写抓取代码,您将通过交互式会话获得更好的结果:您将能够手动与浏览器交互(获取 XPath、id 和这样)和命令。

标签: python selenium


【解决方案1】:

如果您希望 chrome 和 chromedriver 保持打开状态,则必须在启动 chromedriver 时使用“分离”选项。

在你的情况下添加:

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

或者您可以在调试模式下运行代码,最后带有断点,当它暂停“杀死”程序并接管浏览器时,如果您愿意,但这仅适用于 IDE。

编辑 - 为清楚起见添加了导入

【讨论】:

  • 你还需要 from "selenium.webdriver.chrome.options import Options"
  • 是否可以重复使用这个浏览器窗口?例如。获取 PID 或其他内容并在存在时显式使用?
  • 对于 Python,您使用 chrome_options.add_experimental_option("detach", True),对于 C#,您使用 ChromeOptions.LeaveBrowserOpen 属性...无论哪种方式,在您将 options.AddExcludedArgument("enable-automation") 添加到您的选项设置之前,它们都不会起作用。
【解决方案2】:

您也可以像这样添加global browser

def open_instagram():    
    try:
        # Connect
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument("--incognito")
        global browser # this will prevent the browser variable from being garbage collected
        browser = webdriver.Chrome('drivers/chromedriver.exe', chrome_options=chrome_options)
        browser.set_window_size(1800, 900)
        browser.get("https://www.instagram.com/accounts/login/?hl=de")
        browser.find_element(By.NAME, 'username').send_keys('MYEMAIL', Keys.TAB, 'MYPW', Keys.ENTER)
    except Exception as e:
        print (e, 'Instagram')

open_instagram()

Source

【讨论】:

    【解决方案3】:

    硒 4 / PHP / Docker

    $this->driver = RemoteWebDriver::createBySessionID(self::$session_id, self::$server, 60000, 60000);

    version: "3.5"
    #Latest version
    networks:
      grid-network:
    
    services:
    
      selenium-hub:
        image: selenium/hub:latest
        container_name: selenium-hub
        ports:
          - "4446:4444"
        networks:
          - grid-network
          
      chrome:
        shm_size: 4gb 
        image: selenium/standalone-chrome:latest
        container_name: chrome
        depends_on:
          - selenium-hub
        environment:
          - NODE_MAX_SESSION=5
          - NODE_MAX_INSTANCES=5
          - GRID_MAX_SESSION=31556926
          - GRID_BROWSER_TIMEOUT=31556926
          - GRID_TIMEOUT=31556926
          - GRID_SESSION_TIMEOUT=31556926
          - SESSION_TIMEOUT=31556926
          - NODE_SESSION_TIMEOUT=31556926
          - GRID_CLEAN_UP_CYCLE=31556926
          - SE_NODE_SESSION_TIMEOUT=31556926
          - SE_SESSION_REQUEST_TIMEOUT=31556926
        volumes:
          - /dev/shm:/dev/shm
        ports:
          - "33333:5900"
          - "3333:7900"
          - "44444:4444"
        links:
          - selenium-hub
        networks:
          - grid-network
    

    【讨论】:

      猜你喜欢
      • 2011-08-21
      • 2019-09-16
      • 1970-01-01
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多