如何使用python在Selenium中以编程方式使Firefox无头?

如何使用python在Selenium中以编程方式使Firefox无头?,第1张

如何使用python在Selenium中以编程方式使Firefox无头?

要不费吹灰之力地调用Firefox浏览器,可以

headless
通过以下
Options()
设置属性:

from selenium import webdriverfrom selenium.webdriver.firefox.options import Optionsoptions = Options()options.headless = Truedriver = webdriver.Firefox(options=options, executable_path=r'C:UtilityBrowserDriversgeckodriver.exe')driver.get("http://google.com/")print ("Headless Firefox Initialized")driver.quit()

还有另一种方法可以完成无头模式。如果你需要禁用或启用Firefox中的无头模式,而无需修改代码,您可以设置环境变量

MOZ_HEADLESS
,以 什么
,如果你想Firefox的运行无头,或根本不设置它。

例如,在使用持续集成并且希望在服务器中运行功能测试但仍能够在PC上以正常模式运行测试时,此功能非常有用。

$ MOZ_HEADLESS=1 python manage.py test # testing example in Django with headless Firefox

要么

$ 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


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/4947165.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-13
下一篇 2022-11-13

发表评论

登录后才能评论

评论列表(0条)

保存