机器人框架中的Chrome选项

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了机器人框架中的Chrome选项相关的知识,希望对你有一定的参考价值。

我正在尝试从网页上的链接下载文件。但是,我收到恼人的警告“此文件类型可能会危害……反正?保留,丢弃”。我尝试了几种选择来避免发出警告,但仍然得到警告。我正在使用机器人框架,但是我正在使用python为我创建新关键字。

@keyword('open "${url}" in chrome browser')
    def open_chrome_browser(self, url):
        options = webdriver.ChromeOptions()
        options.add_argument("--start-maximized")
        options.add_argument("--disable-web-security")
        options.add_argument("--allow-running-insecure-content")
        options.add_argument("--safebrowsing-disable-extension-blacklist")
        options.add_argument("--safebrowsing-disable-download-protection")
        prefs = {'safebrowsing.enabled': 'true'}
        options.add_experimental_option("prefs", prefs)
        self.open_browser(url, 'chrome',alias=None, remote_url=False, desired_capabilities=options.to_capabilities(), ff_profile_dir=None)

有人可以建议禁用下载警告的方法。非常感谢。

答案

我通过一些研究找到了答案。由于某些原因(可能是错误),open_browser并未设置chrome的功能。因此,替代方法是使用“ create_webdriver”。使用以下代码:

@keyword('open "${url}" in chrome browser')
def open_chrome_browser(self, url):
    options = webdriver.ChromeOptions()
    options.add_argument("--start-maximized")
    options.add_argument("--disable-web-security")
    options.add_argument("--allow-running-insecure-content")
    options.add_argument("--safebrowsing-disable-extension-blacklist")
    options.add_argument("--safebrowsing-disable-download-protection")
    prefs = {'safebrowsing.enabled': 'true'}
    options.add_experimental_option("prefs", prefs)
    instance = self.create_webdriver('Chrome', desired_capabilities=options.to_capabilities())
    self.go_to(url)
另一答案

您需要在列表中添加所有参数。然后将此列表传递给Dictionary对象,并将其传递给打开的浏览器。例如

${list} =     Create List    --start-maximized    --disable-web-security
${args} =     Create Dictionary    args=${list}
${desired caps} =     Create Dictionary    platform=${OS}     chromeOptions=${args}
Open Browser    https://www.google.com    remote_url=${grid_url}    browser=${BROWSER}    desired_capabilities=${desired caps}
另一答案

[请完整阅读我的解释,最好不要禁用浏览器“只是为了解决一个问题”所附带的任何安全功能或任何其他默认值,最好根本不触摸它来找到解决方案。] >

我建议不使用浏览器:),只使用python中的请求模块并使用相同的关键字,以后再在所有代码库中都可以使用。采用这种方法的原因是,最好使用ubiquotois模块来完成工作,而不是将时间花费在一个模块上花费大量时间,我以前经常这样做,更好地安装了请求+ robotframework-requests库和其他只是完成工作。

只需使用下面的代码在其中创建一个关键字,然后在任何需要的地方调用它,而无需费心解决浏览器行为的麻烦。

import requests

file_url = "http://www.africau.edu/images/default/sample.pdf"

r = requests.get(file_url, stream=True)

with open("sample.pdf", "wb") as pdf:
    for chunk in r.iter_content(chunk_size=1024):

        # writing one chunk at a time to pdf file
        if chunk:
            pdf.write(chunk)

以上是关于机器人框架中的Chrome选项的主要内容,如果未能解决你的问题,请参考以下文章

当片段具有选项菜单时,Android Chrome Cast 介绍 Overlay 的行为不正确

使用 Robot 框架在 Linux 机器上执行 headless chrome 浏览器

关于侧边导航菜单和片段的一般设计问题

ActionBarSherlock、ViewPager、TabsAdapter 嵌套选项卡片段

Android 逆向使用 Python 解析 ELF 文件 ( Capstone 反汇编 ELF 文件中的机器码数据 | 创建反汇编解析器实例对象 | 设置汇编解析器显示细节 )(代码片段

Android - 一个选项卡中的多个片段