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 headless mode you need to:
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: