您可在Selen上,如以下所示。 * -headless=new
较好,因为-headless
https://www.selenium.dev/blog/2023/headless-is-ding-away/“rel=”nofollow noreferer” Headless is Going Away!:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--headless=new") # Here
driver = webdriver.Chrome(options=options)
Or:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--headless=new") # Here
driver = webdriver.Chrome(options=options)
In addition, the examples below can test Django Admin with headless Chrome, Selenium, pytest-django and Django. *My answer explains how to test Django Admin with multiple headless browsers(Chrome, Microsoft Edge and Firefox), Selenium, pytest-django and Django:
# "tests/test.py"
import pytest
from selenium import webdriver
from django.test import LiveServerTestCase
@pytest.fixture(scope="class")
def chrome_driver_init(request):
options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
chrome_driver = webdriver.Chrome(options=options)
request.cls.driver = chrome_driver
yield
chrome_driver.close()
@pytest.mark.usefixtures("chrome_driver_init")
class Test_URL_Chrome(LiveServerTestCase):
def test_open_url(self):
self.driver.get(("%s%s" % (self.live_server_url, "/admin/")))
assert "Log in | Django site admin" in self.driver.title
Or:
# "tests/conftest.py"
import pytest
from selenium import webdriver
@pytest.fixture(scope="class")
def chrome_driver_init(request):
options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
chrome_driver = webdriver.Chrome(options=options)
request.cls.driver = chrome_driver
yield
chrome_driver.close()
# "tests/test.py"
import pytest
from django.test import LiveServerTestCase
@pytest.mark.usefixtures("chrome_driver_init")
class Test_URL_Chrome(LiveServerTestCase):
def test_open_url(self):
self.driver.get(("%s%s" % (self.live_server_url, "/admin/")))
assert "Log in | Django site admin" in self.driver.title