Window keeps closing after running selenium

北城余情 提交于 2020-08-20 07:49:11

问题


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?


回答1:


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:

  • Selenium webdriver: Modifying navigator.webdriver flag to prevent selenium detection
  • Is there a way to use Selenium WebDriver without informing the document that it is controlled by WebDriver?

tl; dr

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




回答2:


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()



回答3:


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



来源:https://stackoverflow.com/questions/60128965/window-keeps-closing-after-running-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!