Selenium working with Chrome, but not headless Chrome

后端 未结 6 732
渐次进展
渐次进展 2020-12-10 03:44

I\'ve developed a couple of Python scripts using Selenium and, at first, PhantomJS. While heading toward automated downloads, I switched for (headed) Firefox (which worked)

相关标签:
6条回答
  • 2020-12-10 04:01

    refer https://github.com/SeleniumHQ/selenium/issues/4477 add below code

    self.chrome_options = webdriver.ChromeOptions()
    self.chrome_options.add_argument("--window-size=1920,1080")
    self.chrome_options.add_argument("--disable-extensions")
    self.chrome_options.add_argument("--proxy-server='direct://'")
    self.chrome_options.add_argument("--proxy-bypass-list=*")
    self.chrome_options.add_argument("--start-maximized")
    self.chrome_options.add_argument('--headless')
    self.chrome_options.add_argument('--disable-gpu')
    self.chrome_options.add_argument('--disable-dev-shm-usage')
    self.chrome_options.add_argument('--no-sandbox')
    self.chrome_options.add_argument('--ignore-certificate-errors')
    self.browser = webdriver.Chrome(options=self.chrome_options)
    
    0 讨论(0)
  • I had the same issue and setting the window size in conftest.py solved it. My code snippet:

    @pytest.fixture
    def chrome_options(chrome_options, pytestconfig):
        if pytestconfig.getoption('headless'):
            chrome_options.add_argument('--headless')
            chrome_options.add_argument("window-size=1920,1080")
        else:
            chrome_options.add_argument("start-maximized");
        return chrome_options
    
    0 讨论(0)
  • 2020-12-10 04:05

    Headless Chrome does not support insecure certificates and hence, websites with insecure certificates does not open living it blank. You need to add capabilities as follow:

    from selenium import webdriver
    from selenium.webdriver import DesiredCapabilities
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    
    capabilities = DesiredCapabilities.CHROME.copy()
    capabilities['acceptSslCerts'] = True 
    capabilities['acceptInsecureCerts'] = True
    
    driver = webdriver.Chrome(chrome_options = chrome_options,executable_path='your path',desired_capabilities=capabilities)
    driver.get("yourWebsite")
    

    This will do the work.

    0 讨论(0)
  • 2020-12-10 04:11

    For some if you remove the below, it would work.

    driver.fullscreen_window()
    
    0 讨论(0)
  • 2020-12-10 04:12

    Headless chrome may be faster on same machine than headed, try adding some wait before locating password element.

    0 讨论(0)
  • 2020-12-10 04:20

    I had a same experience like you, and solved it by using xvfb and pyvirtualdisplay.

    I use chromedrive=v2.3.1, chrome-browser=v60 and Selenium=3.4.3

    In Headless chrome, some of script seems not to work as expected.

    Please refer to vpassapera's comment in https://gist.github.com/addyosmani/5336747.

    How about try it like below,

    from pyvirtualdisplay import Display
    
    display = Display(visible=0, size=(800, 600))
    display.start()
    
    # Do Not use headless chrome option
    # options.add_argument('headless')
    
    url = 'https://10.11.227.21/tmui/'
    driver.get(url + "login.jsp")
    
    html_source = driver.page_source
    print(html_source)
    
    blocStatus = WebDriverWait(driver,    TIMEOUT).until(EC.presence_of_element_located((By.ID, "username")))
    inputElement = driver.find_element_by_id("username")
    inputElement.send_keys('actualLogin')
    inputElement = driver.find_element_by_id("passwd")
    inputElement.send_keys('actualPassword')
    inputElement.submit()
    
    display.stop()
    

    xvfb is required to use "pyvortualdisplay"

    $ sudo apt-get install -y xvfb 
    
    0 讨论(0)
提交回复
热议问题