How to keep chrome browser window open to be re-us

2019-06-06 19:21发布

This question already has an answer here:

I am trying to keep the chrome browser open after selenium finishes executing my test script. I want to re-use the same window for my second script to run.

3条回答
Ridiculous、
2楼-- · 2019-06-06 20:05

I know what to do in WATIR(Ruby language), I am writing the code below, So it might give you the clue what to do with your language

require 'watir'
caps = Selenium::WebDriver::Remote::Capabilities.chrome(chrome_options: {detach: true})
b = Watir::Browser.new :chrome, desired_capabilities: caps
b.goto('www.google.co.uk')

This given below line is important, If you can re-write this line your language(python),then you may prevent from closing chrome browser

caps = Selenium::WebDriver::Remote::Capabilities.chrome(chrome_options: {detach: true})
查看更多
做自己的国王
3楼-- · 2019-06-06 20:10

Browser window closes when your Chrome webdriver instance variable is garbage collected. If you want to avoid this even when your script finishes executing, you can make it global. I.e.:

def test():
    global driver # this will prevent the driver variable from being garbage collected
    driver = webdriver.Chrome()
    ...

Explanation: A selenium.webdriver.Chrome class instance contains an instance of a Service class. The latter has a __del__ method which is called when the instance is being destructed during garbage collection process. The method in turn stops the service and causes Chrome browser window to close.

This also explains why some users don't observe this behavior. I suspect that this is because they have Chrome webdriver instance variable at file scope, not inside a function.

查看更多
三岁会撩人
4楼-- · 2019-06-06 20:19

This should be as simple as not calling driver.quit() at the end of your test case. You should be left with the chrome window in an opened state.

查看更多
登录 后发表回答