Headless chrome with selenium, can only find ways to scroll non-headless

自作多情 提交于 2019-12-08 01:33:24

Found the answer after 2 days trying different combinations of versions of selenium, chrome and chromedriver i alsmost gave up and wanted to go with the xvfb.

Already tried to maximze the window in the chrome arguments, that didn't help. But this time i tried setting a manual window size. That helped.

    chrome_options.add_argument("window-size=1920,1080")

Posting here so that the next one wont take as long as me.

I just ran into this problem on windows. Using chrome 74 and i fixed the issue by having the below chromeOptions. My headless mode works again :) Thanks at DebanjanB

chromeOptions.addArguments("--headless")
chromeOptions.addArguments("--no-sandbox")
chromeOptions.addArguments("--disable-dev-shm-usage")
chromeOptions.addArguments("--window-size=1920x1080")
chromeOptions.addArguments("start-maximised")

To scroll to the end of the page of a (not so long) infinity scroll through Default Chrome Browser and Headless Chrome Browser you can use the following code block :

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

options = Options()
options.add_argument("--headless")
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('http://www.website.com')

while (driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")):
    try:
        WebDriverWait(driver, 50).until(EC.visibility_of_element_located((By.XPATH, "//div[@itemprop='itemListElement']" )))
        # do your other actions within the Viewport
    except TimeoutException:
        break
print("Reached to the bottom of the page")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!