The Dan
The Dan

Reputation: 1690

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

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/')

Upvotes: 7

Views: 25852

Answers (3)

na_sacc
na_sacc

Reputation: 878

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')

Upvotes: 0

user1090944
user1090944

Reputation: 465

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()

Upvotes: 1

arnaud1050
arnaud1050

Reputation: 156

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/')

Upvotes: 10

Related Questions