xdmojojojodx
xdmojojojodx

Reputation: 23

Window keeps closing after running selenium

Everytime I run this code the window opens blank and then loads the required page for about 1 second before closing.

from selenium import webdriver

driver = webdriver.Chrome('C:/Users/*****/Downloads/chromedriver_win32/chromedriver.exe')
driver.get("https://stackoverflow.com/")

An error has come up once or twice saying [268:10204:0208/163438.782:ERROR:broker_win.cc(55)] Error reading broker pipe: The pipe has been ended. (0x6D) but it only appears sometimes even though the code hasn't changed.

Any suggestions?

Upvotes: 1

Views: 6665

Answers (4)

Subhankar Chakraborty
Subhankar Chakraborty

Reputation: 179

If you want your code to keep open your browser window then you can do the following which I mentioned in this post: Chrome browser closes immediately after loading from selenium

#if you find this effective then do mark it as useful

Thank You

Subhankar Chakraborty

Upvotes: 0

Vineeth Kasula
Vineeth Kasula

Reputation: 1

One of the best possible workarounds is to set Sleep(), so that the browser doesn't close:

import time 

driver.get('https://www.google.com')
time.sleep(3000)  #this makes browser not to close.

Additional reading https://selenium-python.readthedocs.io/waits.html

Upvotes: 0

Towernter
Towernter

Reputation: 275

I am using ChromeDriver 81.0.4044.138 placed in C:\Windows and this is whats working for me

from selenium import webdriver

class Stackoverflow(object):
    def __init__(self):
        self.options = webdriver.ChromeOptions() 
        self.options.add_experimental_option('useAutomationExtension', False)
        self.options.add_experimental_option("excludeSwitches", ["enable-automation"])
        self.driver = webdriver.Chrome(options=self.options)
        self.driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
          "source": """
            Object.defineProperty(navigator, 'webdriver', {
              get: () => undefined
            })
          """
        })
        self.driver.execute_cdp_cmd("Network.enable", {})
        self.driver.execute_cdp_cmd("Network.setExtraHTTPHeaders", {"headers": {"User-Agent": "browser"}})
        self.driver.get("https://www.stackoverflow.com/")

if __name__ == '__main__':
    Stackoverflow()

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193308

This error message...

ERROR:broker_win.cc(55)] Error reading broker pipe: The pipe has been ended. (0x6D)

...implies that the pipe is broken as if the browser side has been closed.

This error is defined in broker_win.cc within the Chromium code repository as follows:

Channel::MessagePtr WaitForBrokerMessage(PlatformHandle platform_handle,
                     BrokerMessageType expected_type) {
  char buffer[kMaxBrokerMessageSize];
  DWORD bytes_read = 0;
  BOOL result = ::ReadFile(platform_handle.handle, buffer,
               kMaxBrokerMessageSize, &bytes_read, nullptr);
  if (!result) {
    // The pipe may be broken if the browser side has been closed, e.g. during
    // browser shutdown. In that case the ReadFile call will fail and we
    // shouldn't continue waiting.
    PLOG(ERROR) << "Error reading broker pipe";
    return nullptr;
  }

The main reason you see this error is because the ChromeDriver controlled Chrome browser gets detected and the navigation gets blocked.


Solution

As a solution you may need to configure the ChromeDriver / Chrome with certain configurations so Selenium driven Chrome Browsing Context doesn't get detected.


References

You can find a couple of relevant detailed discussions in:


tl; dr

Broken pipe error selenium webdriver, when there is a gap between commands?

Upvotes: 1

Related Questions