How can I execute a "scrollIntoView()" function if the headless of the webdriver is equals to True? How can I execute a "scrollIntoView()" function if the headless of the webdriver is equals to True? selenium selenium

How can I execute a "scrollIntoView()" function if the headless of the webdriver is equals to True?


scrollIntoView() must work identically irrespective of Options().headless = True or Options().headless = False.

However, while using mode you need to:

  • Maximize the browsing window

    options = Options()options.add_argument("--headless")options.add_argument("window-size=1400,600")
  • Additionally induce WebDriverWait for visibility_of_element_located() as follows:

    from selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ECelement = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "reference")))driver.execute_script("arguments[0].scrollIntoView();", element)

References

You can find a relevant detailed discussion in: