selenium ChromeDriver desired_capabilities已弃用,请传入一个带有选项kwarg的Options对象

col17t5w  于 2023-02-08  发布在  其他
关注(0)|答案(2)|浏览(580)

当我启动Selenium webdriver.Remote时,我收到了这个弃用警告。在python中,我的Selenium版本是selenium==4.0.0b2.post1

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

那个Option对象应该是什么?我怎么声明它?
这是我的代码:

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

nhhxz33t1#

您可以按以下方式使用“选项”(Options)而不是“所需功能”(DesiredCapabilities):

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

fslejnso2#

对于在MacOS上运行的Selenium,您可以使用如下选项:

from selenium import webdriver

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

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

driver.quit()

对于在Windows上运行的Selenium,您可以使用如下选项:

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

如果您正在使用Appium自动化,这对我很有效:

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

相关问题