2

I'm trying to execute a code that scrolls down an inner div to a certain element. That just works if:

Options().headless = False

But as you guys know, that isn't good to the performance of the whole thing.

The code that do the scroll is:

element = driver1.find_element_by_xpath(reference)
driver1.execute_script("arguments[0].scrollIntoView();", element)

How can I do something like that, but with the headless equals to True?

gusta
  • 69
  • 7

1 Answers1

2

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 WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    element = 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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352