AttributeError:'Options' object has no attribute 'self' error using ChromeOptions for headless Google Chromethrough Selenium Python

ddrv8njm  于 9个月前  发布在  Python
关注(0)|答案(2)|浏览(70)

所以我一直在努力让无头Chrome工作了几天。我不知道是什么错了!!我已经尝试了我能在论坛上找到的所有与这个问题有关的东西。
现在这是我正在运行的代码(它是从别人的教程中直接摘录的,对他们来说很好):

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

browser_name = "chrome"
if browser_name == 'chrome':
    options = webdriver.ChromeOptions()
    options.headless = True
    driver = webdriver.Chrome(executable_path=r"./chromedriver", options=options)
    start_url = "https://google.com"
    driver.get(start_url)
    print(driver.page_source.encode("utf-8"))
    driver.quit()

字符串
当我运行该代码时,我收到以下错误

driver = webdriver.Chrome(executable_path=r"./chromedriver", options=options)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 64, in __init__
    desired_capabilities = options.self.to_capabilities()
AttributeError: 'Options' object has no attribute 'self'


这可能是值得知道的chromedriver是在正确的路径,我知道这一点,因为当我运行:

browser_name = "chrome"

 if browser_name == 'chrome':
    
    driver = webdriver.Chrome(r"./chromedriver")
    start_url = "https://google.com"
    driver.get(start_url)
    print(driver.page_source.encode("utf-8"))
    driver.quit()


这工程罚款

knpiaxh1

knpiaxh11#

有两种不同的方法。如果您使用:

options = webdriver.ChromeOptions()

字符串
仅使用:

from selenium import webdriver


就足够了。
但是如果你使用的是下面的导入:

from selenium.webdriver.chrome.options import Options


必须使用Options()的示例将headless属性设置为**True**,如下所示:

options = Options()
options.headless = True
driver = webdriver.Chrome(executable_path=r"./chromedriver", options=options)

nhn9ugyo

nhn9ugyo2#

我在使用Robot-Framework SeleniumLibrary打开浏览器时遇到了类似的错误:

robot.errors.HandlerExecutionFailed: AttributeError: 'Options' object has no attribute ''

字符串
显然,options参数是一个用;分隔的字符串,不应该以;结尾,比如:
“-
一旦我删除了最后一个;,浏览器就启动了,并带有所需的选项。

相关问题