使用Selenium+Python3操作Opera:页面不跳转及AttributeError报错求助
Opera浏览器Selenium驱动问题排查与解决
环境信息
- Opera版本:95.0.4635.46(x86_64),基于Chromium 109.0.5414.120
- 系统:macOS
- 初始使用ChromeDriver版本:109.0.5414.74
问题1:Opera打开后不访问目标链接,地址栏显示data:,
原因
尽管Opera基于Chromium,但直接用ChromeDriver启动Opera时,缺少Opera专属的远程调试端口参数,导致Selenium无法与浏览器建立有效的调试会话,无法执行导航指令。
解决方法
添加--remote-debugging-port参数指定可用端口,让Selenium能接管浏览器会话。修改后的代码如下:
import time from selenium import webdriver from selenium.webdriver.chrome.service import Service opera_options = webdriver.ChromeOptions() opera_options.binary_location = "/Applications/Opera.app/Contents/MacOS/Opera" opera_options.add_argument("start-maximized") opera_options.add_argument("--disable-extensions") opera_options.add_argument("--disable-gpu") opera_options.add_argument("--disable-dev-shm-usage") opera_options.add_argument("--no-sandbox") # 添加远程调试端口,确保Selenium能连接Opera opera_options.add_argument("--remote-debugging-port=9222") service = Service(executable_path='./chromedriver') opera_driver = webdriver.Chrome(service=service, options=opera_options) opera_driver.get("https://www.google.com") time.sleep(10) opera_driver.quit()
问题2:使用OperaDriver时出现AttributeError: 'Service' object has no attribute 'process'
原因
OperaDriver版本与当前使用的Selenium版本不兼容,旧版OperaDriver未适配Selenium 4.x的Service类结构,导致调用时出现属性缺失错误。
解决方法
- 确保OperaDriver版本与Opera浏览器版本严格匹配(需下载对应Chromium 109.x分支的OperaDriver)
- 使用Selenium专属的
OperaService类替代Chrome的Service类,代码示例:
import time from selenium import webdriver from selenium.webdriver.opera.service import Service as OperaService opera_options = webdriver.ChromeOptions() opera_options.binary_location = "/Applications/Opera.app/Contents/MacOS/Opera" opera_options.add_argument("start-maximized") opera_options.add_argument("--disable-extensions") opera_options.add_argument("--disable-gpu") opera_options.add_argument("--disable-dev-shm-usage") opera_options.add_argument("--no-sandbox") # 使用Opera专属的Service类 service = OperaService(executable_path='./operadriver') opera_driver = webdriver.Opera(service=service, options=opera_options) opera_driver.get("https://www.google.com") time.sleep(10) opera_driver.quit()
- 若问题仍存在,可调整Selenium版本至与OperaDriver兼容的稳定版本(如Selenium 4.3.0)
内容的提问来源于stack exchange,提问作者WeatherMon
相关产品推荐
相关产品推荐

