Window keeps closing after running selenium

前端 未结 3 1468
遥遥无期
遥遥无期 2021-01-25 16:43

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 = w         


        
相关标签:
3条回答
  • 2021-01-25 17:26

    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()
    
    0 讨论(0)
  • 2021-01-25 17:33

    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?

    0 讨论(0)
  • 2021-01-25 17:37

    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

    0 讨论(0)
提交回复
热议问题