Window keeps closing after running selenium

前端 未结 3 1473
遥遥无期
遥遥无期 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()
    

提交回复
热议问题