首页 > 解决方案 > 使用 RobotFramework 将现有的 Chrome 浏览器与远程调试端口一起使用

问题描述

我正在尝试使用 RobotFramework (SeleniumLibrary) 使用现有的 google chrome 实例。我正在像这样启动 chrome 实例

chrome.exe --remote-debugging-port=9289 --user-data-dir="D:\gcdata"

这是我在机器人框架中的代码

${options}= Evaluat      sys.modules['selenium.webdriver'].ChromeOptions()  sys,selenium.webdriver  
${prefs}=       Create Dictionary   debuggerAddress     127.0.0.1:9289
Call Method    ${options}           add_experimental_option    prefs    ${prefs}
Create WebDriver    Chrome  chrome_options=${options}       

当我运行 RobotFramework 代码时,它会调用一个新的浏览器。任何人都可以在这里帮助我告诉我出了什么问题以及如何解决它。

标签: selenium-webdriverselenium-chromedriverrobotframework

解决方案


使用最新版本的 Python Selenium 模块、Chrome 和 ChromeDriver,以下机器人脚本将连接到已运行的 chrome,该 chrome 使用以下命令启动:

chrome.exe --remote-debugging-port=9289 --user-data-dir="C:\temp\gdata"

chrome_debugger.robot

*** Settings ***
Library    SeleniumLibrary  
Library    Collections      

*** Test Cases ***
TC

    ${ChromeOptions}=     Evaluate      sys.modules['selenium.webdriver'].ChromeOptions()  sys,selenium.webdriver 

    # Method debugger_address is not callable so convert to Capabilities Dictionary and set it manually
    ${ChromeCapabilities}=     Call Method     ${ChromeOptions}    to_capabilities
    Set To Dictionary    ${ChromeCapabilities["goog:chromeOptions"]}    debuggerAddress    127.0.0.1:9289

    # Instead of using the Chrome Options use Capabilities.
    Create WebDriver    Chrome    desired_capabilities=${ChromeCapabilities}
    Go To    http://cnn.com

即使ChromeOptions类 ( GitHub ) 具有该debugger_address(self, value)方法,从 Robot Framework 调用此方法也会返回错误。因此,将ChromeOptions类转换为Capabilities字典并手动将其添加到字典中,然后再通过参数将其传递给 webdriver desired_capabilities


推荐阅读