Chrome 在用 selenium 填写表单后立即自行关闭

Chrome closed itself immediately after filling the form in selenium

我现在正在使用 selenium,下面的代码工作正常,但在填写表单后,我的 chrome 浏览器会立即自行关闭。我该如何防止这种情况发生。我不希望浏览器自行关闭。

代码如下

from lib2to3.pgen2 import driver
from sre_parse import State
from tkinter.tix import Select
from selenium import webdriver

from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
import time

time.sleep(5)

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

driver.get("https://ssg2021.in/citizenfeedback")

# selcting states
state_select = driver.find_element(By.ID,'State')
drp1 = Select(state_select)

drp1.select_by_visible_text('Chhattisgarh')


# selecting district
district_select = driver.find_element(By.XPATH,'//*[@id="State"]')
drp2 = Select(district_select)

drp2.select_by_index(10)



Selenium 总是在代码完成后自动退出 运行。您可以添加一个 time.sleep() 以使其保持打开状态

from lib2to3.pgen2 import driver
from sre_parse import State
from tkinter.tix import Select
from selenium import webdriver

from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
import time

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

driver.get("https://ssg2021.in/citizenfeedback")

# selcting states
state_select = driver.find_element(By.ID,'State')
drp1 = Select(state_select)

drp1.select_by_visible_text('Chhattisgarh')


# selecting district
district_select = driver.find_element(By.XPATH,'//*[@id="State"]')
drp2 = Select(district_select)

drp2.select_by_index(10)
time.sleep(30)

您也可以通过添加 'detach' 选项来实现,如下所示:

from lib2to3.pgen2 import driver
from sre_parse import State
from tkinter.tix import Select
from selenium import webdriver

from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import time

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), chrome_options=chrome_options)

driver.get("https://ssg2021.in/citizenfeedback")

# selcting states
state_select = driver.find_element(By.ID,'State')
drp1 = Select(state_select)

drp1.select_by_visible_text('Chhattisgarh')


# selecting district
district_select = driver.find_element(By.XPATH,'//*[@id="State"]')
drp2 = Select(district_select)

drp2.select_by_index(10)