Forums

My selenium script doesn't run in headless mode

I have this Python script. It doesn't run in headless mode, because it can't render the page.

How can I use selenium without headless mode on Python Anywhere, or what other services can I use?

# Replace 'path_to_webdriver' with the actual path to your ChromeDriver executable
driver = webdriver.Chrome()

url = 'https://www.24liveblog.com/live/3422333916194946648'
try:
    driver.get(url)  # Open the webpage in the browser

    # Wait for the live blog content to load (you might need to adjust the timeout value)
    time.sleep(20)

    # Get the webpage content after JavaScript execution
    webpage_content = driver.page_source
    log.write(f"Succesfully downloaded {url}.\n")
except Exception as e:
    log.write(f"Failed to download {url}: {e}\n")

Could you try the snippet from this help page using suggested selenium version for your system image?

Oh, got it to work. Thanks!

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")

# Create a new WebDriver with the specified service and options
driver = webdriver.Chrome(options=chrome_options)

url = 'https://www.24liveblog.com/live/3422333916194946648'

try:
    driver.get(url)  # Open the webpage in the browser

    #Give web page time to load
    time.sleep(20)

    # Get the webpage content after JavaScript execution
    webpage_content = driver.page_source
finally:
    driver.quit()  # Close the web browser

Thanks for letting us know!