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
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()
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.
As a solution you may need to configure the ChromeDriver / Chrome with certain configurations so Selenium driven Chrome Browsing Context doesn't get detected.
You can find a couple of relevant detailed discussions in:
Broken pipe error selenium webdriver, when there is a gap between commands?
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