如何将变量传递给python(selenium Webdriver)包函数?

How to pass a variable to python (selenium Webdriver) package function?

我有以下要求。我应该能够动态地向要传递的参数提供端口号

1
webdriver.remote.webdriver.WebDriver(<PARAMS HERE>)

我试过很多种组合,但没能搞清楚

1
2
command ="command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=webdriver.DesiredCapabilities.HTMLUNIT"
driver = webdriver.remote.webdriver.WebDriver(command)

我得到以下错误:

1
2
3
4
5
6
7
Traceback (most recent call last):
  File"sel2.py", line 38, in <module>
  driver = webdriver.remote.webdriver.WebDriver(command)

  File"/home/sysqe/my36project/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 134, in __init__
    raise WebDriverException("Desired Capabilities can't be None")
selenium.common.exceptions.WebDriverException: Message: Desired Capabilities can't be None

它应该能够插入var命令并将参数放在适当的位置。


类似这样的内容可能适用于关键字args:

1
2
3
4
5
command = {
    command_executor: 'http://127.0.0.1:4444/wd/hub',
    desired_capabilities: webdriver.DesiredCapabilities.HTMLUNIT
}
driver = webdriver.remote.webdriver.WebDriver(**command)

见:

  • 将python dict转换为kwargs?