how to override default set of chrome command line switches in selenium

前端 未结 5 1122
遇见更好的自我
遇见更好的自我 2021-01-06 09:34

By default, chrome will be run with this command line:

\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\"
--disable-hang-monitor
--disable-         


        
5条回答
  •  别那么骄傲
    2021-01-06 10:06

    You should start browser by yourself and then tell selenium, that you already have it launched via passing special channel id. Something like that:

    from random import randrange
    channel_id = "%032x" % randrange(16**32)
    
    from subprocess import Popen
    # HERE YOU PASS ONLY THOSE PARAMETERS YOU WANT (i.e. without --disable-*)
    # BUT YOU MAY NEED --dom-automation FOR SOME ROUTINES
    chrome = Popen(" ".join([
        PATH_TO_CHROME_EXE,
        "--no-first-run", "--dom-automation",
        ("--testing-channel=\"NamedTestingInterface:%s\"" % channel_id),
    ]))
    
    try:
        from selenium.webdriver.chrome.service import Service
        chromedriver_server = Service(PATH_TO_CHROMEDRIVER, 0)
        chromedriver_server.start()
        from selenium.webdriver import Remote
        driver = Remote(chromedriver_server.service_url,
            {"chrome.channel": channel_id, "chrome.noWebsiteTestingDefaults": True})
    
        driver.get(MY_WEBPAGE)
        # DO YOUR WORK
    
    finally:
        chromedriver_server.stop()
        driver.quit()
    
    chrome.kill()
    chrome.wait()
    

提交回复
热议问题