4 回答

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超8個(gè)贊
在您的 WebDriver 中(當(dāng)您實(shí)例化它時(shí)),您可以將以下內(nèi)容添加到 Chrome 選項(xiàng)中
chrome_options.add_experimental_option("detach", True)
完成此操作后,通過(guò)命令終端(Windows 中的命令提示符)運(yùn)行它,它應(yīng)該不會(huì)關(guān)閉
主程序 - 供參考
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()

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
硒 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

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊
driver.close()它關(guān)閉是因?yàn)槟谀┪蔡砑恿藘?nèi)容。只要?jiǎng)h除該行,瀏覽器就會(huì)永遠(yuǎn)保持打開(kāi)狀態(tài)。如果你想在一段時(shí)間后關(guān)閉它,那么你可以像這樣添加time.sleep之前:driver.close()
import time
# Your code
time.sleep(60) #Stays open for 60 seconds (which is 1 min)
driver.close()
- 4 回答
- 0 關(guān)注
- 251 瀏覽
添加回答
舉報(bào)