Tin*_*Zea 88 python selenium python-3.x selenium-webdriver firefox-headless
我用python,selenium和firefox运行这段代码,但仍然得到firefox的'head'版本:
binary = FirefoxBinary('C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe', log_file=sys.stdout)
binary.add_command_line_options('-headless')
self.driver = webdriver.Firefox(firefox_binary=binary)
Run Code Online (Sandbox Code Playgroud)
我也尝试了二进制的一些变体:
binary = FirefoxBinary('C:\\Program Files\\Nightly\\firefox.exe', log_file=sys.stdout)
binary.add_command_line_options("--headless")
Run Code Online (Sandbox Code Playgroud)
Deb*_*anB 163
要无头调用Firefox浏览器,可以headless通过Options()类设置属性,如下所示:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("http://google.com/")
print ("Headless Firefox Initialized")
driver.quit()
Run Code Online (Sandbox Code Playgroud)
还有另一种方法可以实现无头模式.如果您需要在Firefox中禁用或启用无头模式,而无需更改代码,则可以将环境变量设置MOZ_HEADLESS为任何内容,如果您希望Firefox无头运行,或者根本不设置它.
当您使用例如持续集成并且您希望在服务器中运行功能测试但仍能够在PC中以正常模式运行测试时,这非常有用.
$ MOZ_HEADLESS=1 python manage.py test # testing example in Django with headless Firefox
Run Code Online (Sandbox Code Playgroud)
要么
$ export MOZ_HEADLESS=1 # this way you only have to set it once
$ python manage.py test functional/tests/directory
$ unset MOZ_HEADLESS # if you want to disable headless mode
Run Code Online (Sandbox Code Playgroud)
小智 10
我的答案:
set_headless(headless=True) is deprecated.
Run Code Online (Sandbox Code Playgroud)
options.headless = True
Run Code Online (Sandbox Code Playgroud)
为我工作
Phi*_*eil 10
第一个答案不再起作用。
这为我工作:
from selenium.webdriver.firefox.options import Options as FirefoxOptions
options = FirefoxOptions()
options.add_argument("--headless")
driver = webdriver.Firefox(options=options)
driver.get("http://google.com")
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以在 Python 中使用Selenium运行无头 Firefox ,如下所示:
from selenium import webdriver
options = webdriver.FirefoxOptions()
options.add_argument("-headless") # Here
driver = webdriver.Firefox(options=options)
Run Code Online (Sandbox Code Playgroud)
或者:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument("-headless") # Here
driver = webdriver.Firefox(options=options)
Run Code Online (Sandbox Code Playgroud)
此外,下面的示例可以使用 headless Firefox、Selenium、pytest-django和Django来测试 Django Admin 。*我的回答解释了如何使用多个无头浏览器(Chrome、Microsoft Edge 和 Firefox)、Selenium、pytest-django 和 Django 测试 Django Admin:
# "tests/test_1.py"
import pytest
from selenium import webdriver
from django.test import LiveServerTestCase
@pytest.fixture(scope="class")
def firefox_driver_init(request):
options = webdriver.FirefoxOptions()
options.add_argument("-headless")
firefox_driver = webdriver.Firefox(options=options)
request.cls.driver = firefox_driver
yield
firefox_driver.close()
@pytest.mark.usefixtures("firefox_driver_init")
class Test_URL_Firefox(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
Run Code Online (Sandbox Code Playgroud)
或者:
# "tests/conftest.py"
import pytest
from selenium import webdriver
@pytest.fixture(scope="class")
def firefox_driver_init(request):
options = webdriver.FirefoxOptions()
options.add_argument("-headless")
firefox_driver = webdriver.Firefox(options=options)
request.cls.driver = firefox_driver
yield
firefox_driver.close()
Run Code Online (Sandbox Code Playgroud)
# "tests/test_1.py"
import pytest
from django.test import LiveServerTestCase
@pytest.mark.usefixtures("firefox_driver_init")
class Test_URL_Firefox(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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
77935 次 |
| 最近记录: |