我們通常會(huì)使用Selenium編寫UI測(cè)試,為瀏覽器開啟Headless模式(執(zhí)行操作時(shí)不顯示GUI窗口)會(huì)很方便。最新版本的Chrome和Firefox中,均已支持headless模式。
在Selenium中,為這兩個(gè)瀏覽器開啟headless模式的方式基本相同:
Chrome:
| from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('headless') driver = webdriver.Chrome(options=options) |
Firefox:
| from selenium import webdriver options = webdriver.FirefoxOptions() options.add_argument('headless') driver = webdriver.Firefox(options=options) |
我提交的PR#5120添加了和Chrome相同的導(dǎo)入接口,如果你使用Selenium小于3.8.0版本,則需要將上面的webdriver.FirefoxOptions()
替換為webdriver.firefox.options.Options()
。
另外,你也可以使用環(huán)境變量MOZ_HEADLESS
來為Firefox開啟headless模式:
| import os from selenium import webdriver os.environ['MOZ_HEADLESS'] = '1' # <- this line driver = webdriver.Firefox() |
本文基于我在Stack Overflow的這篇回答:https://stackoverflow.com/a/47481793/5511849
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。