如何在 Robot Framework 中将 Selenium 选项作为数据结构发送而不会出现“AttributeError: 'list' object has no attribute 'capability'”?

问题描述 投票:0回答:1

我想使用数据结构将选项发送到 Selenium 开放浏览器界面。

该库的文档表明它接受“Any”类型,因此假定允许使用列表或字典或任何组合。

将选项作为长字符串发送确实有效。

add_argument("--no-sandbox"); add_argument("--disable-web-security"); add_argument("--disable-software-rasterizer"); add_argument("--proxy-server=http://10.10.10.10:80") 

但不将其作为数据结构发送,例如:

[{'add_argument': ['--no-sandbox']}, {'add_argument': ['--disable-web-security']}, {'add_argument': ['--disable-software-rasterizer']}, {'add_argument': ['--proxy-server=http://10.10.10.10:80']}]

这里的错误信息是:

AttributeError: 'list' object has no attribute 'capabilities' 

我尝试了许多其他数据结构,但没有一个有效。

设置为:

机器人框架7.0

RobotFramework-SeleniumLibrary 6.2.0

硒4.18.1

Python 3.12

${arguments}     Create List
${options}       Create List
Append To List  ${options}  --no-sandbox
Append To List  ${options}  --disable-web-security
Append To List  ${options}  --disable-software-rasterizer
Append To List  ${options}  --proxy-server=http://10.10.10.10:80
FOR  ${elem}  IN   @{options}
  ${elem_list}   Create list  ${elem}
  ${dict}  Create Dictionary  add_argument  ${elem_list}
  Append To List  ${arguments}  ${dict}
END
Log To Console    ${arguments}
${current_open_window}=  Open Browser  browser=Chrome  options=add_argument("--no-sandbox"); add_argument("--disable-web-security"); add_argument("--disable-software-rasterizer"); add_argument("--proxy-server=http://10.10.10.10:80")
selenium-webdriver robotframework
1个回答
0
投票

通过使用对象来解决。

from selenium import webdriver

class seleniumOptions:
  def get_options(self):
    options = webdriver.ChromeOptions()
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-web-security')
    options.add_argument('--disable-software-rasterizer')
    options.add_argument('--proxy-server=10.10.10.10:80')
    return options
© www.soinside.com 2019 - 2024. All rights reserved.