简体   繁体   English

如何在运行程序时保持 Selenium Webdriver chrome 浏览器打开?

[英]How to keep Selenium Webdriver chrome browser open when running a program?

I am using selenium to extract data from a sports website.我正在使用 selenium 从体育网站提取数据。 I want my Chrome browser to remain open until I close it.我希望我的 Chrome 浏览器在关闭之前保持打开状态。 But my program closes the Chrome window after 3-4 seconds.但是我的程序在 3-4 秒后关闭了 Chrome window。 Any help on finding a solution would be much appreciated.任何有关寻找解决方案的帮助将不胜感激。

Here's my code:这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import  Keys
import pandas as pd

print('\nWelcome to Arsenal FC players payroll page\n')
page_num = input('Enter the year for payroll data (2011-2020): ')

df = pd.DataFrame(columns = ['Player', 'Salary', 'Year']) #creates a master dataframe


driver = webdriver.Chrome('/Users/mahtabkhan/Documents/chromedriver')

if(page_num != 2020):
    url = 'https://www.spotrac.com/epl/arsenal-fc/payroll/' + page_num + '/'
else:
    url = 'https://www.spotrac.com/epl/arsenal-fc/payroll/' 

driver.get(url)

players = driver.find_elements_by_xpath('//td[@class="player"]')
salaries = driver.find_elements_by_xpath('//td[@class="cap info"]')

#to get the text of each player into a list
players_list = []
for p in range(len(players)):
    players_list.append(players[p].text)

#to get the salaries into a list
salaries_list = []
for s in range(len(salaries)):
    salaries_list.append(salaries[s].text)  

data_tuples = list(zip(players_list[1:],salaries_list[1:])) # list of each players name and salary paired together
temp_df = pd.DataFrame(data_tuples, columns=['Player','Salary']) # creates dataframe of each tuple in list
temp_df['Year'] = page_num   # adds season beginning year to each dataframe
df = df.append(temp_df)  #appends to master dataframe


driver.close()

It closes because you have added driver.close() at the end.它关闭是因为您在最后添加了driver.close() Just remove that line and the browser would stay open forever.只需删除该行,浏览器将永远保持打开状态。 If you want to close it after some time, then you can add time.sleep before driver.close() like this:如果你想在一段时间后关闭它,那么你可以在driver.close()之前添加time.sleep ,如下所示:

import time

# Your code

time.sleep(60) #Stays open for 60 seconds (which is 1 min)

driver.close()

In your WebDriver ( when you're instantiating it ), you can add the following to your Chrome Options在您的 WebDriver 中(当您实例化它时),您可以将以下内容添加到您的 Chrome 选项中

chrome_options.add_experimental_option("detach", True)

Once you do that, run it via the command terminal ( Command Prompt in Windows ) and it should not close on you完成此操作后,通过命令终端(Windows 中的命令提示符)运行它,它不应关闭

MAIN PROGRAM - For Reference主程序 - 供参考

from selenium import webdriver

def get_chrome_driver():
    """This sets up our Chrome Driver and returns it as an object"""
    path_to_chrome = "F:\Selenium_Drivers\Windows_Chrome85_Driver\chromedriver.exe"
    chrome_options = webdriver.ChromeOptions() 
    
    # Keeps the browser open
    chrome_options.add_experimental_option("detach", True)
    
    # Browser is displayed in a custom window size
    chrome_options.add_argument("window-size=1500,1000")
    
    # Removes the "This is being controlled by automation" alert / notification
    chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
    
    return webdriver.Chrome(executable_path = path_to_chrome,
                            options = chrome_options)



# Gets our chrome driver and opens our site
chrome_driver = get_chrome_driver()
chrome_driver.get("https://www.google.com/")
print('The browser should not close after you see this message')
chrome_driver.service.stop()

Selenium 4 \/ PHP \/ Docker硒 4 \/ PHP \/ 码头工人

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

version: "3.5"
#Latest version
networks:
  grid-network:

services:

  selenium-hub:
    image: selenium/hub:latest
    container_name: 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
    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

driver.close() will close your browser. driver.close() 将关闭您的浏览器。 just remove it if you want your browser still open如果您希望浏览器仍然打开,只需将其删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Selenium WebDriver和python打开功能齐全的Chrome浏览器? - How to open a fully functioning chrome browser using selenium WebDriver with python? 使用 Headless Chrome Webdriver 运行 Selenium - Running Selenium with Headless Chrome Webdriver 使用 Selenium webdriver 模块时无法打开 Firefox 浏览器 - Firefox browser does not open when using Selenium webdriver module 如何使用 selenium webdriver 和 python 关闭 chrome 浏览器弹出对话框 - how to close chrome browser popup dialog using selenium webdriver and python 如何在脚本中打开程序并继续运行脚本? - How to open a program in a script and keep running the script? 如何使用 Selenium WebDriver (Chrome) 打开页面,而无需等待它在 Python 中加载? - How to open a page with Selenium WebDriver (Chrome), without waiting for it to load in Python? 如何使用python的Selenium WebDriver在浏览器上打开一个新的window? - How to open a new window on a browser using Selenium WebDriver for python? Python selenium 保持浏览器打开 - Python selenium keep browser open 由python webdriver在chrome浏览器中的selenium新选项卡 - selenium new tab in chrome browser by python webdriver 无法使用 Selenium for Python 打开 Chrome WebDriver - Unable to Open Chrome WebDriver with Selenium for Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM