通过Python和Selenium控制Opera



我想通过Python + Selenium自动操作Opera。

通过参考下面的URL,我在下面写了一个脚本。

驾驶歌剧与硒蟒

from selenium import webdriver
from selenium.webdriver.chrome import service

webdriver_service = service.Service('/usr/bin/opera')         
webdriver_service.start()
capabilities = { 'operaOptions': { 'debuggerAddress': "localhost:1212" }}
browser = webdriver.Remote(webdriver_service.service_url, webdriver.DesiredCapabilities.OPERA)
response = browser.get('https://www.facebook.com/')

通过此脚本,Opera启动,但Facebook页面无法打开。

我应该如何解决?请帮助我。

设置如下。

乌班图16.04,蟒蛇 3.5.2,硒 3.6.0,歌剧 49.0

capabilities = { 'operaOptions': { 'debuggerAddress': "localhost:1212" }}

由于您正在设置此设置,因此我假设您需要此才能访问网络。但是,您没有将其设置为Web驱动程序功能

capabilities = webdriver.DesiredCapabilities.OPERA
capabilities ['operaOptions'] = { 'debuggerAddress': "localhost:1212" }
browser = webdriver.Remote(webdriver_service.service_url, capabilities)

这是一个简单易行的方法:

from selenium import webdriver
driver = webdriver.Opera()
driver.get("https://www.facebook.com")
# make sure 'operadriver.exe' is in the same folder as your script

如果在查找歌剧二进制文件时遇到问题:

from selenium import webdriver
options = webdriver.ChromeOptions()
options.binary_location ="<your opera executable directory>"
driver = webdriver.Opera(options=options)
driver.get("https://www.facebook.com")
# make sure 'operadriver.exe' is in the same folder as your script