【问题标题】:Roboframework - SeleniumLibrary - set specific user profile path for ChromeRoboframework - SeleniumLibrary - 为 Chrome 设置特定的用户配置文件路径
【发布时间】:2021-06-04 21:52:04
【问题描述】:

我的目标是使用 SeleniumLibrary(或 Selenium2Library)创建一个 Roboframework 脚本,为 Chrome 选择一个特定的配置文件路径(我需要重用配置文件)。

这是我正在使用的代码:

*** Settings ***
Library           Selenium2Library
Library           OperatingSystem

*** Test Cases ***
Test Profile
    ${options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
    ${options.add_argument}=    Set Variable    --user-data-dir=/tmp/.org.chromium.Chromium.Test
    Create WebDriver    Chrome    chrome_options=${options}
    Go To    chrome://version  
    Wait Until Page Contains    JavaScript
    Capture Page Screenshot

如果我运行脚本,我不会收到任何错误,但不幸的是,chrome 路径不受所需设置的影响(一直在创建 root+随机路径):

过去我已经使用 Python 代码和 Selenium Grid 成功实现了这一点:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-notifications')
chrome_options.add_argument('--user-data-dir=/tmp/.com.google.Chrome.Test') 
browser = webdriver.Remote(command_executor='http://192.168.99.100:4444/wd/hub',desired_capabilities = chrome_options.to_capabilities())

感谢任何帮助/评论。 非常感谢

【问题讨论】:

    标签: selenium selenium-webdriver robotframework


    【解决方案1】:

    试试下面的方法

    *** Settings ***
    Library           Selenium2Library
    Library           OperatingSystem
    
    *** Test Cases ***
    Test Profile
        ${options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
        Call Method    ${options} add_argument --user-data-dir=/tmp/.org.chromium.Chromium.Test
        Create WebDriver    Chrome    chrome_options=${options}
        Go To    chrome://version  
        Wait Until Page Contains    JavaScript
        Capture Page Screenshot
    

    如果这不起作用,请尝试不使用--,即

    试试下面的方法

    *** Settings ***
    Library           Selenium2Library
    Library           OperatingSystem
    
    *** Test Cases ***
    Test Profile
        ${options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
        Call Method    ${options} add_argument user-data-dir=/tmp/.org.chromium.Chromium.Test
        Create WebDriver    Chrome    chrome_options=${options}
        Go To    chrome://version  
        Wait Until Page Contains    JavaScript
        Capture Page Screenshot
    

    【讨论】:

    • Thx,在这两种情况下都出现此错误:调用方法“add_argument”失败:TypeError:add_argument() 得到了意外的关键字参数“--user-data-dir”或调用方法“add_argument”失败: TypeError: add_argument() got an unexpected keyword argument 'user-data-dir'
    • 这与 Robot 框架的一些语法有关,但方法应该围绕这一行本身。我会检查我能找到确切的语法
    • Thx Tarun,请看我的回复,我让它使用不同的语法
    【解决方案2】:

    我用这个语法让它工作:

    ${chrome_options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys
        Call Method     ${chrome options}    add_argument    user-data-dir\=/tmp/.org.chromium.Chromium.Test
        ${dc}   Evaluate    sys.modules['selenium.webdriver'].DesiredCapabilities.CHROME  sys, selenium.webdriver
        ${Options}=    Call Method    ${ChromeOptions}    to_capabilities
        Create WebDriver    Chrome    chrome_options=${chrome_options}  desired_capabilities=${dc}
        Go To    chrome://version  
        Wait Until Page Contains    JavaScript
        Capture Page Screenshot
    

    【讨论】:

      猜你喜欢
      • 2019-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 2017-06-02
      • 2019-12-20
      相关资源
      最近更新 更多