Selenium:如何使 geckodriver 无头

Noa*_*iff 6 selenium selenium-webdriver

我已经完成了 geckodriver 的代码,现在想知道如何使 geckodriver 无头。之前看到一个帖子,内容如下:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=options, 
executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
print("Firefox Headless Browser Invoked")
driver.get('http://google.com/')
driver.quit()
Run Code Online (Sandbox Code Playgroud)

我不明白 webdriver 下选项的下载来自哪里。当我下载 geckodriver 时,附带的只是可执行文件。任何帮助是极大的赞赏!!

Ron*_*ris 5

对我有用。我使用的步骤: (1) 打开命令提示符并导航到包含 geckodriver.exe 的文件夹。geckodriver.exe(2)从命令提示符处启动,不带任何选项。(3) 打开另一个命令提示符并键入python并按 Return 键。(4) 将以下代码复制/粘贴到您的 python 会话中。

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver import Firefox

options = Options()
options.add_argument("--headless")
# Don't put the path to geckodriver in the following. But the firefox executable
# must be in the path. If not, include the path to firefox, not geckodriver below.
driver = Firefox(firefox_options=options)

print("Firefox Headless Browser Invoked")
driver.get('http://google.com/')

# Print the first 300 characters on the page.
print(driver.page_source[:300])
driver.quit()
Run Code Online (Sandbox Code Playgroud)