0

我正在尝试使用 python selenium 库启动歌剧。但获取功能错误。

我尝试过的代码:

代码1:

driver = webdriver.Opera()
driver.get('https://www.google.com')

代码2:

driver = webdriver.Opera(r'path to operadriver.exe')
driver.get('https://www.google.com')

代码3:

options = Options()
options.binary_location = r'C:\Opera\launcher.exe'
driver = webdriver.Opera(options=options)
driver.get('https://www.google.com')

输出:

代码1:

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Opera binary

代码2:

selenium.common.exceptions.WebDriverException: Message: Desired Capabilities must be a dictionary

代码3:

[20904:3220:0120/034255.122:ERROR:os_crypt_win.cc(61)] Failed to decrypt: The parameter is incorrect. (0x57)

DevTools listening on ws://127.0.0.1:59016/devtools/browser/0bb7bc3c-4b9a-451a-a736-a02a63feba7a
[20904:3220:0120/034255.673:ERROR:CONSOLE(0)] "Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.", source: chrome://startpage/ (0)
[20904:3220:0120/034255.674:ERROR:CONSOLE(0)] "Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.", source: chrome://startpage/ (0)
[20904:3220:0120/034255.675:ERROR:CONSOLE(0)] "Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.", source: chrome://startpage/ (0)

只有代码 3 的 Opera 浏览器启动器。但是网址打不开。

因为我能够使用类似的代码启动 chrome。

4

2 回答 2

1

此错误消息...

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Opera binary

和这个错误信息......

selenium.common.exceptions.WebDriverException: Message: Desired Capabilities must be a dictionary

和这个错误信息......

[20904:3220:0120/034255.122:ERROR:os_crypt_win.cc(61)] Failed to decrypt: The parameter is incorrect. (0x57)

...暗示OperaDriver无法启动/产生新的浏览上下文,即Opera 浏览器会话。


解决方案

首先,您需要确保您已经从Operasoftware / operachromiumdriver下载了最新的OperaChromiumDriver。根据基于 Chromium 的 Opera 版本的 OperaDriver

OperaChromiumDriver 是从ChromeDriver派生并由Opera改编的 WebDriver 实现,可以为桌面和 Android 平台实现基于 Chromium 的 Opera 产品的编程自动化。它是Selenium项目的一部分。

OperaChromiumDriver 无需额外设置即可在基于 Chromium 的 Opera 版本 26 上使用。

使用这两个参数:

  • binary_location对于歌剧二进制和
  • executable_path用于操作驱动程序二进制
  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.opera.options import Options
    
    options = Options()
    options.binary_location = r'C:\Opera\launcher.exe'
    driver = webdriver.Opera(options=options, executable_path=r'C:\path\to\operadriver.exe')
    driver.get("http://google.com/")
    
于 2020-01-19T22:50:15.350 回答
0

这对我有用:

from selenium import webdriver
from selenium.webdriver.opera.options import Options

options = Options()
driver = webdriver.Opera(options=options)
driver.get("https://www.google.com")

我也遇到了与您显示的相同的错误,但是我需要打开的 URL 正在 Opera 中自动加载而没有问题。

您需要确保您的 Opera 版本与驱动程序的版本匹配。打开opera检查它并输入:opera://about 确保operadriver.exe在python脚本的同一个文件夹中。

驱动可以在这里下载: https ://github.com/operasoftware/operachromiumdriver/releases

于 2020-03-28T18:48:34.293 回答