【bug】selenium AttributeError: ‘NoneType’ object has no attribute ‘to_capabilities’
环境
linux
python
selenium 4.24.0
详情
首次在linux使用 selenium 进行测试时弹出这个错误,这个错误是由于webdriver.Remote
没有设置浏览器参数options
导致的错误
Traceback (most recent call last):
File "a.py", line 6, in <module>
driver = webdriver.Remote(
^^^^^^^^^^^^^^^^^
File "/data/data/miniconda3/lib/python3.12/site-packages/selenium/webdriver/remote/webdriver.py", line 192, in __init__
capabilities = options.to_capabilities()
^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'to_capabilities'
解决方法
解决方法是设置options,或者就设置一个空白的Options参数对象也可以。下面是一个样例
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium import webdriver
chrome_opt = ChromeOptions()
chrome_opt.add_argument('--headless') # 添加无界面浏览器参数
driver = webdriver.Remote(
command_executor="http://localhost:4444",
options=chrome_opt
)
driver.get("http://www.baidu.com")
print(driver.title)
driver.close()
运行输入:百度一下,你就知道
参考
https://wenku.csdn.net/answer/34034b44fb13457ebcb2f05e54483c57