为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
当年话下 2023-11-09 10:48:11
我正在使用硒从体育网站提取数据。我希望 Chrome 浏览器保持打开状态,直到我将其关闭。但我的程序在 3-4 秒后关闭 Chrome 窗口。任何有关寻找解决方案的帮助将不胜感激。这是我的代码:from selenium import webdriverfrom selenium.webdriver.common.keys import  Keysimport pandas as pdprint('\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 dataframedriver = 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 listplayers_list = []for p in range(len(players)):    players_list.append(players[p].text)#to get the salaries into a listsalaries_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 togethertemp_df = pd.DataFrame(data_tuples, columns=['Player','Salary']) # creates dataframe of each tuple in listtemp_df['Year'] = page_num   # adds season beginning year to each dataframedf = df.append(temp_df)  #appends to master dataframedriver.close()
查看完整描述

4 回答

?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

在您的 WebDriver 中(当您实例化它时),您可以将以下内容添加到 Chrome 选项中


chrome_options.add_experimental_option("detach", True)

完成此操作后,通过命令终端(Windows 中的命令提示符)运行它,它应该不会关闭


主程序 - 供参考


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()


查看完整回答
反对 回复 2023-11-09
?
BIG阳

TA贡献1859条经验 获得超6个赞

硒 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


查看完整回答
反对 回复 2023-11-09
?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

driver.close()它关闭是因为您在末尾添加了内容。只要删除该行,浏览器就会永远保持打开状态。如果你想在一段时间后关闭它,那么你可以像这样添加time.sleep之前:driver.close()


import time


# Your code


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


driver.close()


查看完整回答
反对 回复 2023-11-09
?
精慕HU

TA贡献1845条经验 获得超8个赞

driver.close() 将关闭您的浏览器。如果您希望浏览器仍然打开,只需将其删除



查看完整回答
反对 回复 2023-11-09
  • 4 回答
  • 0 关注
  • 163 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信