设置浏览器下载目录不生效

540 阅读1分钟

「这是我参与11月更文挑战的第7天,活动详情查看:2021最后一次更文挑战」今天遇到一个很奇怪的问题,同样的脚本,同样版本的浏览器,脚本在我的电脑运行正常,浏览器的下载目录能够设置成功,在同事的电脑里运行就不行,一直是浏览器默认的下载目录

分析过程

原因

牺牲自己的摸鱼时间,终于将原因找出来了, selenium的webdriver的 desired_capabilities参数在搞鬼,同事用的python版本是3.7.5, 这个参数在3.7.5版本源码文件里标明是弃用的,但是你用的时候不会像弃用参数chrome_options使用的时候给你个提示

python3.7.5 源码

Args: 
- executable_path - Deprecated: path to the executable. If the default is used it assumes the executable is in the $PATH 
- port - Deprecated: port you would like the service to run, if left as 0, a free port will be found. 
- options - this takes an instance of ChromeOptions 
- service_args - Deprecated: List of args to pass to the driver service 
- desired_capabilities - Deprecated: Dictionary object with non-browser specific capabilities only, such as "proxy" or "loggingPref". 
- service_log_path - Deprecated: Where to log information from the driver. - keep_alive - Deprecated: Whether to configure ChromeRemoteConnection to use HTTP keep-alive.
'''

而在python3.7.9版本中,这个参数不是弃用的,可以正常使用的

"""
Creates a new instance of the chrome driver.

Starts the service and then creates new instance of chrome driver.

:Args:
 - executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH
 - port - port you would like the service to run, if left as 0, a free port will be found.
 - options - this takes an instance of ChromeOptions
 - service_args - List of args to pass to the driver service
 - desired_capabilities - Dictionary object with non-browser specific
   capabilities only, such as "proxy" or "loggingPref".
 - service_log_path - Where to log information from the driver.
 - chrome_options - Deprecated argument for options
 - keep_alive - Whether to configure ChromeRemoteConnection to use HTTP keep-alive.
"""

而脚本里,是使用desired_capabilities这个参数来设置下载目录的

   ${prefs}    create dictionary    download.default_directory=${currentpath}${/}Doc${/}    download.prompt_for_download=False
    log   ${prefs}
    Call Method    ${driver_options}    add_experimental_option    prefs   ${prefs}
    ${options}=    Call Method    ${driver_options}    to_capabilities
    Create Webdriver    ${bro}    desired_capabilities=${options}

所以同事用python3.7.5运行脚本虽然能够正常运行,但是下载目录却不会设置成功,后面改成使用options参数来设置下载目录,两个python版本都能够运行成功