![](/img/trans.png)
[英]AttributeError: 'Options' object has no attribute 'add_experimental_option'
[英]How to fix 'Options' object has no attribute 'add_experimental_option' in Selenium Python
環境:
Python 3.10
硒==4.3.0
Windows 10
問題
我正在嘗試使用 Chrome 制作一個 selenium 腳本。
chrome_options = Options()
chrome_options.add_argument(r"--user-data-dir=" + chrome_profile_path)
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-application-cache")
chrome_options.add_argument("--disable-session-crashed-bubble")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--start-maximized")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
我從我的 Windows 終端執行代碼。 它產生了這個問題:
chrome_profile_path : C:\Users\gauth\AppData\Local\Google\Chrome\User Data
You don't have Chrome browser installed on your computer or we di'nt find your profile folder! : 'Options' object has no attribute 'add_experimental_option'
因此,在同一個終端中,我執行了 python 並在下面輸入了這些行以重現該問題:
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
我沒有遇到任何問題。
那么,為什么我在執行腳本時會出現問題,而在 Python 終端中逐行執行時卻不會出現問題?
這是完整的代碼:
def ChromeDriverWithProfile():
"""
This function will return the chrome driver with the Chrome profile loaded
"""
try:
if platform.system() == 'Linux':
chrome_profile_path = os.environ['HOME'] + "/.config/google-chrome"
chrome_profile_path = os.path.expandvars(chrome_profile_path)
elif platform.system() == 'Darwin':
if not os.environ['HOME'] or os.environ['HOME'] == "":
HOME_DIR = os.path.expanduser("~")
else:
HOME_DIR = os.environ['HOME']
chrome_profile_path = HOME_DIR + "/Library/Application Support/Google/Chrome/Default"
chrome_profile_path = os.path.expandvars(chrome_profile_path)
else:
chrome_profile_path = r"%LocalAppData%\Google\Chrome\User Data"
chrome_profile_path = os.path.expandvars(chrome_profile_path)
print(f"chrome_profile_path : {chrome_profile_path}")
chrome_options = Options()
chrome_options.add_argument(r"--user-data-dir=" + chrome_profile_path)
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-application-cache")
chrome_options.add_argument("--disable-session-crashed-bubble")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--start-maximized")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"]) # !!!! THE ISSUE COME HERE AT THIS LINE !!!!
chrome_options.add_experimental_option('useAutomationExtension', False)
# overcome limited resource problems
chrome_options.add_argument("--disable-dev-shm-usage")
# Bypass OS security model
chrome_options.add_argument("--no-sandbox")
# We need to remove the bubble popup 'Restore pages' of Chrome:
# https://dev.to/cuongld2/get-rid-of-chrome-restore-bubble-popup-when-automate-gui-test-using-selenium-3pmh
if platform.system() == 'Linux':
preference_file = chrome_profile_path + "/Default/Preferences"
elif platform.system() == 'Darwin':
preference_file = chrome_profile_path
else:
preference_file = chrome_profile_path + "\\Default\\Preferences"
string_to_be_change = '"exit_type":"Crashed"'
new_string = '"exit_type": "none"'
# read input file
fin = open(preference_file, "rt")
# read file contents to string
data = fin.read()
# replace all occurrences of the required string
data = data.replace(string_to_be_change, new_string)
# close the input file
fin.close()
# open the input file in write mode
fin = open(preference_file, "wt")
# overrite the input file with the resulting data
fin.write(data)
# close the file
fin.close()
driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
return driver
except Exception as ex:
print(
f"You don't have Chrome browser installed on your computer or we di'nt find your profile folder! : {ex}")
return None
driver = ChromeDriverWithProfile()
我在web上搜索了這個問題“'Options' object has no attribute 'add_experimental_option'”。沒有任何東西。 顯然,我是地球上唯一遇到此問題的人。 :-(
你能幫我嗎?
我最近遇到了類似的情況。 問題是我輸錯了下載文件夾的全名。 也許您想檢查一下。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.