Home > Net >  Selenium is quitting web browser after scrolling
Selenium is quitting web browser after scrolling

Time:07-14

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.

CodePudding user response:

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)
  • Related