嗯,我正在开发一个项目,该项目使用 selenium 来获取一些电影文件并从网站下载它们。一切工作正常,但我不想打开浏览器窗口并希望它在后台运行并最终显示结果而不调用浏览器。我尝试了很多方法,但未能找到可靠的答案。 StackOverflow 上有与此相关的问题,但它们不适用于 Edge。其他问题的答案讲述了如何添加这一行。
from selenium.webdriver.Edge.options import Options
options = Options()
options.headless = True
driver = webdriver.Edge(executable_path="msedgedriver.exe", options=options)
但这对我不起作用,并且弹出以下错误。
File "movies.pyx", line 11, in init movies
driver = webdriver.Edge(executable_path="msedgedriver.exe", options=options)
TypeError: __init__() got an unexpected keyword argument 'options'
我也尝试过查看类似问题的许多答案,但没有任何效果。如果有人知道请回答我会提前非常感谢你。
我正在使用python 3.7.7
您正在使用 Microsoft Edge Webdriver。 您尝试使其无头的方式适用于 chrome webdriver。
options = EdgeOptions()
options.use_chromium = True
options.add_argument("headless")
options.add_argument("disable-gpu")
通过导入尝试上面的代码:
from msedge.selenium_tools import EdgeOptions
from msedge.selenium_tools import Edge
注意:您必须启用 Chromium 才能启用 Headless。
这不是直接解决问题的方法,而是一种替代方案,我想你可以检查这个简单的库:https://pypi.org/project/activesoup/
activesoup 结合了熟悉的 python Web 功能,方便使用 无头“浏览”功能。 [...] 在以下情况下考虑使用 activesoup: [...] 您需要主动交互 使用 Python 的一些网页(例如 提交表单、下载 文件)
您可以在 Python 中使用 Selenium 运行 Headless Microsoft Edge,如下所示。 *我的回答用Django示例解释了它:
from selenium import webdriver
options = webdriver.EdgeOptions()
options.add_argument("--headless=new") # Here
driver = webdriver.Edge(options=options)
或者:
from selenium import webdriver
from selenium.webdriver.edge.options import Options
options = Options()
options.add_argument("--headless=new") # Here
driver = webdriver.Edge(options=options)