Selenium is quitting web browser after scrolling

Issue

Basic question. I want to use selenium to scroll to the bottom of the webpage but it is quitting shortly after scrolling. I want to stay on the page and be where I scrolled to. Here is the code:

def take_screenshots(url):
    driver = webdriver.Chrome("/Users/jatinshekara/chromedriver")
    driver.get(url)
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(5)

I used the time.sleep() function to make it sleep to make sure it was actually scrolling down. It is scrolling down but just quitting the web browser right after (basically what the code "driver.quit()" does).

Thanks in advance.

Solution

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

You’d want to add detach true and then add the options to your driver. Selenium automatically closes after it’s operation.

driver = webdriver.Chrome("/Users/jatinshekara/chromedriver", options=chrome_options)

Answered By – Arundeep Chohan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published