【问题标题】:chrome options in robot framework机器人框架中的 chrome 选项
【发布时间】:2016-12-11 08:02:22
【问题描述】:

我正在尝试从网页上的链接下载文件。但是我收到烦人的警告“这种类型的文件可能会伤害......无论如何?保留,丢弃”。我尝试了几种选择来避免警告,但仍然得到它。我正在使用机器人框架,但是我正在使用 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)

有人可以建议一种禁用下载警告的方法吗?

【问题讨论】:

    标签: python robotframework selenium2library


    【解决方案1】:

    我通过一些研究找到了答案。出于某种原因(可能是错误),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)
    

    【讨论】:

    • 请分享您获得以下值的链接?我找不到它。 --start-maximized,--disable-web-security -allow-running-insecure-content") --safebrowsing-disable-extension-blacklist") --safebrowsing-disable-download-protectio
    【解决方案2】:

    您需要添加列表中的所有参数。然后将此列表传递给 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}
    

    【讨论】:

      【解决方案3】:

      下面是一个更简单的解决方案:

      打开浏览器 ${URL} ${BROWSER} options=add_argument("--disable-notifications")

      对于多个选项,您可以使用 with ;分开。

      options=add_argument("--disable-popup-blocking"); add_argument("--ignore-certificate-errors")

      【讨论】:

        【解决方案4】:

        最好不要禁用浏览器附带的任何安全功能或任何其他默认设置(除非有充分的理由)“只是为了解决一个问题”,最好不要触摸它来找到解决方案, 和

        只需在 python 中使用 requests 模块并使用与关键字相同的关键字,以后在所有代码库中的任何位置。采用这种方法的原因是,最好使用无处不在的模块完成工作,而不是花大量时间在一个模块上,我以前经常这样做,更好地安装 requests + robotsframework-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)
        

        【讨论】:

          【解决方案5】:

          这对我有用(必须使用SeleniumLibrary 4)。修改 Chrome 以下载 PDF 而不是查看它们:

          ${chrome_options}=  Evaluate  sys.modules['selenium.webdriver'].ChromeOptions()  sys, selenium.webdriver
          ${disabled}  Create List  Chrome PDF Viewer  PrintFileServer
          ${prefs}  Create Dictionary  download.prompt_for_download=${FALSE}  plugins.always_open_pdf_externally=${TRUE}  plugins.plugins_disabled=${disabled}
          Call Method  ${chrome_options}  add_experimental_option  prefs  ${prefs}
          
          ${desired_caps}=  Create Dictionary  browserName=${browserName}  version=${version}  platform=${platform}  screenResolution=${screenResolution}  record_video=${record_video}  record_network=${record_network}  build=${buildNum}  name=${globalTestName}
          
          Open Browser  url=${LOGINURL}  remote_url=${remote_url}  options=${chrome_options}  desired_capabilities=${desired_caps}
          

          【讨论】:

            猜你喜欢
            • 2012-04-24
            • 1970-01-01
            • 2019-03-06
            • 1970-01-01
            • 1970-01-01
            • 2019-05-09
            • 1970-01-01
            • 2020-03-20
            • 2018-04-25
            相关资源
            最近更新 更多