ChromeDriver desired_capabilities has been deprecated, please pass in an Options object with options kwarg
Asked Answered
A

3

7

I am getting this deprecation warning when I start my Selenium webdriver.Remote in python, my selenium version is selenium==4.0.0b2.post1

desired_capabilities has been deprecated, please pass in an Options object with options kwarg

What is that Option object supposed to be? How do I declare it?

This is my code:

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver
import time

driver = webdriver.Remote(
    command_executor='http://localhost:4444/wd/hub',
    desired_capabilities=DesiredCapabilities.CHROME
)

driver.get('http://www.google.com/')
Adsorbate answered 31/3, 2021 at 17:27 Comment(2)
Does this answer your question? How do I pass options to the Selenium Chrome driver using Python?Greengage
Thanks for your answer, but it seems to be unrelated to the question. "selenium.common.exceptions.WebDriverException: Message: Desired Capabilities must be a dictionary "Adsorbate
M
10

You can use Options instead of DesiredCapabilities in the following way:

from selenium import webdriver
import time

driver = webdriver.Remote(
    command_executor='http://localhost:4444/wd/hub',
    options=webdriver.ChromeOptions()
)

driver.get('http://www.google.com/')
Medium answered 24/6, 2021 at 19:21 Comment(1)
What about Safari? There is no webbdriver.SafariOptions.Adrian
J
1

For selenium running on MacOS you can use options like this:

from selenium import webdriver

driver = webdriver.Remote(
    command_executor='http://localhost:4444',
    options=webdriver.FirefoxOptions()
)

driver.get('https://google.com')

driver.quit()

For selenium running on Windows you can use options like this:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.binary_location = r"C:\\Program Files\\Mozilla Firefox\\firefox.exe"

driver = webdriver.Remote(
    command_executor='http://127.0.0.1:4444',
    options=options
)

driver.get('http://google.com')

driver.quit()

If you are using Appium automation, this worked for me:

from appium import webdriver

APPIUM = 'http://localhost:4723'
CAPS = {
    'platformName': 'iOS',
    'platformVersion': '16.2',
    'deviceName': 'iPhone 14',
    'automationName': 'XCUITest',
    'browserName': 'Safari'
}

driver = webdriver.Remote(
    command_executor=APPIUM,
    desired_capabilities=CAPS
)

try:
    driver.get('https://google.com')
finally:
    driver.quit()
Jeddy answered 7/2, 2023 at 1:9 Comment(0)
N
0

You should use Options instead of DesiredCapabilities. It is as follows:

from selenium.webdriver.chrome.options import Options

options = Options()
options.set_capability('pageLoadStrategy', 'eager')

The following an example of calculating the exchange rate for 1 dollar in won. Based on my laptop, there is a difference of about 0.5s when pageLoadStrategy is set to eager and when it is not.

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options as ChromeOptions
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup

def get_usd_to_krw():
    start_time = time.time()
    
    service = ChromeService(ChromeDriverManager().install())
    options = ChromeOptions()
    options.add_argument('--headless')
    options.set_capability('pageLoadStrategy', 'eager')
    driver = webdriver.Chrome(
        service = service,
        options = options
    )
    driver.get('https://www.google.com/search?q=usd+to+krw')
    html = driver.page_source
    soup = BeautifulSoup(html, 'html.parser')
    result = soup.select_one('#knowledge-currency__updatable-data-column > div.b1hJbf > div.dDoNo.ikb4Bb.gsrt > span.DFlfde.SwHCTb').get_text()

    end_time = time.time()
    print(f'delay time: {end_time - start_time:,.2f} secnods')
    return result

print(f'1 dollar → {get_usd_to_krw()}won')
Newmann answered 16/4 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.