python - 使用 Selenium webdriver 模块时 Firefox 浏览器无法打开
问题描述
我期待下面的代码会打开 Firefox 浏览器窗口,但它不会,只会打印我的日志语句。
谁能告诉我我做错了什么?
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time as tm
binary = r'C:\Users\asgar\AppData\Local\Mozilla Firefox\firefox.exe'
options = Options()
options.headless = True
options.binary = binary
cap = DesiredCapabilities().FIREFOX
cap["marionette"] = True #optional
driver = webdriver.Firefox(options=options, capabilities=cap, executable_path=r"C:\Users\asgar\PycharmProjects\firefoxselenium\geckodriver.exe")
driver.get("http://google.com/")
tm.sleep(10)
print ("Headless Firefox Initialized")
driver.quit()
解决方案
尝试这个; 只需将路径更改为“geckodriver.exe”。
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.accept_untrusted_certs = True
wd = webdriver.Firefox(executable_path="C:/your_path/geckodriver.exe", firefox_profile=profile)
url = "https://www.google.com/"
wd.get(url)
那样有用吗?
一方面,'options.headless = True' 看起来很可疑。此外,浏览器右上角的 3 个水平条(打开菜单)下可能有一个设置,用于控制新浏览器窗口打开的行为,因此请查看“打开菜单”中的复选框之一。
推荐阅读
- java - 使用 executeUpdate() 批量更新
- computer-vision - 如何从实时视频源增量重建 3D 场景
- java - 创建 Java 模式
- java - 带有弹出菜单的沉浸式模式 Android API 的错误?
- python - W10 上的 Rust 和 Python:LNK1181
- modal-dialog - 在 Blazor 和 Maui 的单一模式中使用 INotify 接口
- python - 用户名和 request.user 名称具有相同的值,但 django python 不将其识别为相同
- java - 为什么强烈鼓励使用构建器模式,而不是在 Java 中直接实现流畅的 API?
- python - 无法从模块导入子模块
- node.js - Google Compute Engine API:无法从机器映像创建实例