Forums

Selenium won't work

After switching my account to the new virtualization system headless chrome support, I tried to run my code. First, I got the following error:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created

Then, after changing chrome_options to options, I got:

.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from tab crashed
  (Session info: headless chrome=78.0.3904.70)
  (Driver info: chromedriver=2.42.591071 (0b695ff80972cc1a65a5cd643186d2ae582cd4ac),platform=Linux 4.4.0-1100-aws x86_64)

Any help is appreciated.

Is that in a new console or have your reloaded your web app since the update?

It also look like it could be a particular site that is crashing a tab in chrome. Try a generic site like google to see if it's the site that you're trying to access that is causing the problem.

I have reloaded both. I tried running it from the console and running it normally. I also tried the default code with google and I get the same error.

I'm also periodically getting this tab crashed error. I've got a simple scheduled task to snag a screenshot from a specific url and while it runs most of the time, I'm occasionally getting this in the task log:

"... File "/home/stuartasims/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 1055, in save_screenshot return self.get_screenshot_as_file(filename) File "/home/stuartasims/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 1032, in get_screenshot_as_file png = self.get_screenshot_as_png() File "/home/stuartasims/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 1064, in get_screenshot_as_png return base64.b64decode(self.get_screenshot_as_base64().encode('ascii')) File "/home/stuartasims/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 1074, in get_screenshot_as_base64 return self.execute(Command.SCREENSHOT)['value'] File "/home/stuartasims/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute self.error_handler.check_response(response) File "/home/stuartasims/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: session deleted because of page crash from tab crashed (Session info: headless chrome=78.0.3904.70) (Driver info: chromedriver=2.42.591071 (0b695ff80972cc1a65a5cd643186d2ae582cd4ac),platform=Linux 4.4.0-1100-aws x86_64)"

I'm happy to file a separate item if this isn't related to the above error by antidiogofaro.

Context: I used a new bash console after switching to the virtualization system and pip3.8 to install selenium. The script seems to be working like 70% of the time so I'm assuming it's just the headless tab crashing for some reason.

Try using the code in this thread: https://www.pythonanywhere.com/forums/topic/27366/

That definitely works for me @glenn and I used it while getting everything set up initially. My script sets up chrome as below:

from selenium import webdriver
import time

print('execution started ' , time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()))

# start chrome in headless mode and it will run inside the virtual display
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--window-size=1920,1080")
browser = webdriver.Chrome(options= chrome_options)

try:
    browser = webdriver.Chrome(options= chrome_options)
    url = 'some url''
    browser.get(url)
    time.sleep(1)
    browser.save_screenshot(my file directory+'.png')
    print('screenshot successful at ', time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()))

finally:
    browser.quit()

print('ended execution at ',time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()))

I included the "chrome_options.add_argument('--no-sandbox')" due to some discussion here: https://github.com/puppeteer/puppeteer/issues/290#issuecomment-322851507

After running both glenn's and stuartasims code (I was already using a form of glenn's) I keep getting the same errors.

Ok @antidiogofaro. What do you get if you run:

which chromedriver

in a console? Are you running in a virtualenv or using the system installed selenium? What version of Python are you using?

@stuartasims, do you have multiple scheduled tasks that may overlap their execution times? Do you get the exception more when they overlap?

I get the following:

/usr/local/bin/chromedriver

I have no idea if I'm running in a virtualenv or using the installed system. The Python version is 3.8

@glenn, Not that I can think of. I've got it repeating every 15 min (4 separate tasks at different minute values past the hour) but the task logs show execution times are ~20s per run. All tasks run the same .py file.

hi @stuartasims do you mean that with that code you have above, it runs and succeeds sometimes and fails other times?

@antidiogofaro do you have the same problem or does your code always fail?

@conrad my code always fails

@conrad: Yes, exactly. It runs fine ~3/4 of the time (across all the executions, not 3 out of 4 tasks runs well).

@stuartasims - Our task servers are significantly more busy on round numbers of times (like midnight and midday or at 0 minutes past the hour, or 30 minutes past the hour etc.). If you're running on round numbers, you may just be trying to run when the machine is really heavily loaded, so chrome takes longer to start and selenium times out. In that case, offsetting your tasks from the popular times may improve your success rate.

@antidiogofaro - above you were a little unclear about whether you were running in a new console or not. So just to make sure: From the consoles page, start a a new bash console and try the code again there.

@glenn I did what you said. For some reason, @stuartasims code runs perfectly but mine always gives the following error (even if I change my selenium code bits for the same as stuartasims):

 File "/home/antidiogofaro/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
  (unknown error: unable to discover open pages)
  (The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
  (Driver info: chromedriver=2.42.591071 (0b695ff80972cc1a65a5cd643186d2ae582cd4ac),platform=Linux 4.4.0-1100-aws x86_64)

Hi!

I get a similar error and it only occurs from time to time. Could you please point me to what the issue may be? Thanks!

<pre><code>2023-01-06 02:58:57,689: Error running WSGI application 2023-01-06 02:58:57,690: selenium.common.exceptions.SessionNotCreatedException: Message: session not created 2023-01-06 02:58:57,690: from timeout: Timed out receiving message from renderer: 600.000 2023-01-06 02:58:57,690: (Session info: headless chrome=90.0.4430.212) 2023-01-06 02:58:57,690: Stacktrace: 2023-01-06 02:58:57,690: #0 0x55c62dfa8e89 <unknown>

That looks weird. I'd read the message as saying that the page that you've accessed is somehow taking a really long time (if the 600 is seconds, then ten minutes!) to render. Is it possible that sometimes the site you're accessing is returning absolutely huge pages, or something like that?

I wouldn't think so.. I'm looking up company names + "yahoo finance" in Chrome search engine and then clicking on the first link to grab the company ticker, below is the code. Appreciate any pointers!

# Create browser
browser = webdriver.Chrome(options=options) # breaks here?
browser.get('http://www.google.com')
companyname=(input_name)

#'Click' into the google search parameters
search_list = browser.find_elements("name",'q')

# Enter this information
search_list[0].send_keys("yahoo finance "+companyname)
search_list[0].send_keys(Keys.ENTER)
browser.implicitly_wait(15)

# Collect the list of links
link = browser.find_elements(By.XPATH,'//a[starts-with(@href,"https://finance.yahoo")]')
links_as_strings = []

Full error message:

Traceback (most recent call last):
File "/home/pukhova/mysite/scheduled_script.py", line 172, in <module>
  result = daily_run(url, headers)
File "/home/pukhova/mysite/scheduled_script.py", line 168, in daily_run
  result = get_tic(result)
File "/home/pukhova/mysite/scheduled_script.py", line 150, in get_tic
  df.Ticker[i] = name_convert(df.Company[i])
File "/home/pukhova/mysite/scheduled_script.py", line 116, in name_convert
  browser = webdriver.Chrome(options=options)
File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 70, in __init__
  super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 92, in __init__
  RemoteWebDriver.__init__(
File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 275, in __init__
  self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 365, in start_session
  response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 430, in execute
  self.error_handler.check_response(response)
File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
  raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from timeout: Timed out receiving message from renderer: 600.000
  (Session info: headless chrome=90.0.4430.212)
Stacktrace: #0 0x563610e1ae89 <unknown>

Oh how comforting to see I'm not the only one!

I get crashes time to time too. In my local, there has been 0 crashes but in PythonAnywhere, tab crash is like a coin toss. I'm very much irritated of this. How can we get rid of this problem? It's not about driver options, I've already tried everything and as I said, everything works as charm in my local. The problem is heavy load on servers.

By the way, I sometimes get this "600 seconds" timeout as well and it's not about the page.

Which versions of selenium and chromedriver are you using? Are those the same as you use locally?

Ooops, totally forgot about that. Now that I checked, upgrading to 4.7.2 solved my problems, for now. =) Thanks for reminding.

I'm getting this error when trying to run selenium using the ChromeDriverManager package (this used to work in the past on Python Anywhere).

/bin/sh: 1: google-chrome: not found
/bin/sh: 1: google-chrome-stable: not found
/bin/sh: 1: google-chrome-beta: not found
/bin/sh: 1: google-chrome-dev: not found
/bin/sh: 1: google-chrome: not found
/bin/sh: 1: google-chrome-stable: not found
/bin/sh: 1: google-chrome-beta: not found
/bin/sh: 1: google-chrome-dev: not found
Traceback (most recent call last):
  File "/home/jhorrocksHR/surveygizmo/main/alchemer_report_webscrape_completion_counts.py", line 113, in <module>
    results = scape_element(unique_url_list, element_selector)
  File "/home/jhorrocksHR/surveygizmo/main/alchemer_report_webscrape_completion_counts.py", line 94, in scape_element
    with webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options) as driver:
  File "/home/jhorrocksHR/.local/lib/python3.7/site-packages/webdriver_manager/chrome.py", line 40, in install
    driver_path = self._get_driver_binary_path(self.driver)
  File "/home/jhorrocksHR/.local/lib/python3.7/site-packages/webdriver_manager/core/manager.py", line 40, in _get_driver_binary_path
    file = self._download_manager.download_file(driver.get_driver_download_url(os_type))
  File "/home/jhorrocksHR/.local/lib/python3.7/site-packages/webdriver_manager/drivers/chrome.py", line 32, in get_driver_download_url
    driver_version_to_download = self.get_driver_version_to_download()
  File "/home/jhorrocksHR/.local/lib/python3.7/site-packages/webdriver_manager/core/driver.py", line 48, in get_driver_version_to_download
    return self.get_latest_release_version()
  File "/home/jhorrocksHR/.local/lib/python3.7/site-packages/webdriver_manager/drivers/chrome.py", line 64, in get_latest_release_version
    determined_browser_version = ".".join(determined_browser_version.split(".")[:3])
AttributeError: 'NoneType' object has no attribute 'split'

It almost seems to indicate an issue with the package, but hoping someone at PA might be able to help, and perhaps offer some advice?

It looks like it's trying to use google-chrome packages that are not installed on PA. We have chromedriver and chromium available.