使用python函数打开浏览器后,如何保持浏览器处于打开状态?

时间:2018-11-09 11:57:22

标签: robotframework

我有以下python文件OpenBrowser.py

from selenium import webdriver
from selenium.webdriver.chrome.options import Options



def openit(browser):

chrome_options = Options()
chrome_options.add_argument("--headless")

desired_capabilities = chrome_options.to_capabilities()
desired_capabilities['acceptInsecureCerts'] = True


driver = webdriver.Chrome()
#driver = webdriver.Chrome("C:\Python27\Scripts\chromedriver.exe", chrome_options=chrome_options,desired_capabilities=desired_capabilities)
driver.get("http://www.python.org")

return browser

和一个机器人文件:

*** Settings ***
Documentation    Suite description
Library        OpenBrowser.py

*** Test Cases ***
Test title
    openit  browser

浏览器已打开,但随后关闭,如果我想在RF中运行另一个关键字,则会出现错误:没有打开brpwser

如何运行python函数并使浏览器保持打开状态?

1 个答案:

答案 0 :(得分:0)

从我的头顶开始,这应该允许您做您想做的事情:

*** Settings ***
Library    SeleniumLibrary

Suite Teardown    Close All Browsers

*** Test Cases ***
TC
    # Options for startin Chrome
    ${chrome_options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver

    Call Method    ${chrome_options}    add_argument    headless
    Call Method    ${chrome options}    add_argument    ignore-certificate-errors

    # Arguments for starting ChromeDriver
    ${service_args}    Create List
        ...                --verbose
        ...                --log-path=${EXECDIR}/chromedriver.log

    Create Webdriver    Chrome    chrome_options=${chrome_options}    service_args=${service_args} 

    Go To    https://self-signed.badssl.com/

    Capture Page Screenshot

服务参数将指示ChromeDriver在启动Robot Framework的目录中为您生成日志文件。这可能有助于分析。