python selenium crashes in headless [duplicate] - python

This question already has answers here:
unknown error: session deleted because of page crash from unknown error: cannot determine loading status from tab crashed with ChromeDriver Selenium
(9 answers)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created from tab crashed using ChromeDriver Chrome Selenium Python
(2 answers)
Closed 2 years ago.
I know many similar questions have been asked but not a single answer helped me fix my issue.
I have a very basic selenium program in python which seems to crash when I try and run it headless.
Here is what I have so far.
from selenium import webdriver
import time
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--user-agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome("chromedriver", options=options)
driver.get("https://tinder.com/")
driver.set_window_size(1920, 1080)
time.sleep(0.5)
driver.find_element_by_xpath("//*[#id=\"content\"]/div/div[1]/div/main/div[1]/div/div/header/div[1]/div[2]/div/button").click()
currentWindow = driver.window_handles[0]
time.sleep(1)
driver.find_element_by_xpath('//*[#id="modal-manager"]/div/div/div[1]/div/div[3]/span/div[1]/div/button').click()
time.sleep(1)
newWindow = driver.window_handles[1]
driver.switch_to.window(newWindow)
time.sleep(1)
driver.find_element_by_css_selector("input[type=email]").send_keys("someemail#mail.com")
driver.find_element_by_xpath("//*[#id=\"identifierNext\"]/div/button").click()
time.sleep(1)
driver.find_element_by_xpath("//*[#id=\"password\"]/div[1]/div/div[1]/input").send_keys("somepasswordthatyouhave")
driver.find_element_by_xpath("//*[#id=\"passwordNext\"]/div/button").click()
driver.switch_to.window(currentWindow)
# driver.save_screenshot("tinderOpen.png")
time.sleep(5)
driver.save_screenshot("crash.png")
driver.find_element_by_xpath("//*[#id=\"modal-manager\"]/div/div/div/div/div[3]/button[1]").click()
time.sleep(0.5)
driver.find_element_by_xpath("//*[#id=\"modal-manager\"]/div/div/div/div/div[3]/button[1]").click()
driver.save_screenshot("tinderOpened.png")
Now when I run this in the terminal
I get this beautiful error:
Traceback (most recent call last):
File "tinder.py", line 33, in <module>
driver.save_screenshot("crash.png")
File "/home/rodude123/.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/rodude123/.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/rodude123/.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/rodude123/.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/rodude123/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/rodude123/.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=85.0.4183.102)
See, I've tried to use no sandbox and I've even tried disabling dev shm but nothing works. I don't know what to do, please help.
Thanks, rodude123

Related

selenium.common.exceptions.SessionNotCreatedException: Message: session not created, selenium python

I have a problem, every, single, TIME
Basically this is my code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location = "C:\Program Files (x86)\Google\Chrome Beta\Application\chrome.exe"
options.add_argument("--no-sandbox")
options.add_argument("--no-default-browser-check")
options.add_argument("--no-first-run")
options.add_argument("--disable-default-apps")
driver = webdriver.Chrome(options=options, executable_path="C:\Program Files (x86)\Google\Chrome Beta\Application\chromedriver.exe")
driver.get('https://www.youtube.com/')
and the error is
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
I used Pycharm and tried to use VS code with python 3.4 and 3.7 and 3.8.3
plz help me I'm getting tired of this.
Full error Log:
Traceback (most recent call last):
File "C:/Users/um/PycharmProjects/Selenium/main.py", line 10, in <module>
driver = webdriver.Chrome(options=options, executable_path="C:\Program Files (x86)\Google\Chrome Beta\Application\chromedriver.exe")
File "C:\Users\um\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "C:\Users\um\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\um\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\um\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\um\AppData\Local\Programs\Python\Python38-32\lib\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 disconnected: unable to send message to renderer
(Session info: chrome=81.0.4044.83)
Your error is not
unable to send message to renderer
as you mentioned. According to the stack trace you posted the problem in the line 6
driver = webdriver.Chrome(options=options, executable_path="chromedriver.exe", )
and the problem is:
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
It seems like you specified an invalid path to the chromedriver and Selenium is unable to initialize the driver and create a session.
You should also check the version of the chromedriver with the guide.
Here you can download the valid version (I have taken your Chrome version from the stacktrace).
yes it did start and then close with the error log above, and also i have the correct version

Selenium WebDriverException with headless chrome [duplicate]

This question already has answers here:
WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser
(44 answers)
Closed 4 years ago.
I made a python script that scrapes information and clicks a few buttons and it works great on chrome webdriver, but as soon as I try to enter it as headless it does nothing for a few seconds and then gives an error. My code is below.
options = Options()
options.headless = True
options.add_argument("user-data-dir=selenium")
browser = webdriver.Chrome(executable_path=r'C:\Users\REDACTED\Desktop\REDACTED\browser\chromedriver.exe', options=options)
browser.get('https://REDACTED')
And here's the error
Traceback (most recent call last):
File "C:/Users/REDACTED/PycharmProjects/Test/REDACTED.py", line 49, in <module>
browser = webdriver.Chrome(executable_path=r'C:\Users\REDACTED\Desktop\REDACTED\browser\chromedriver.exe', options=options)
File "C:\Users\REDACTED\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "C:\Users\REDACTED\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\REDACTED\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\REDACTED\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\REDACTED\AppData\Local\Programs\Python\Python37\lib\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: DevToolsActivePort file doesn't exist
(Driver info: chromedriver=2.44.609538 (b655c5a60b0b544917107a59d4153d4bf78e1b90),platform=Windows NT 10.0.17134 x86_64)
What works for me when I use selenium with chrome is:
you can try options.add_argument('--headless') as your method brings up an error (only for chrome, no error for firefox)
another thing you can try is removing the .exe from your executable_path. When i add .exe i get a the same error you got.

Unable to Open Chrome WebDriver with Selenium for Python

I am trying the following script and can't get it to open Webdriver:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('/usr/bin/google-chrome')
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()
This produces the following error message:
Traceback (most recent call last):
File "scraper.py", line 4, in <module>
driver = webdriver.Chrome('/usr/bin/google-chrome')
File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 68, in __init__
self.service.start()
File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 98, in start
self.assert_process_still_running()
File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 111, in assert_process_still_running
% (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service /usr/bin/google-chrome unexpectedly exited. Status code was: 1
I'm using Ubuntu 16.04 running on Windows 10. Any ideas what this could be?
EDIT:
Now I'm doing this with chromedriver, which I unzipped to the same directory as the script.
driver = webdriver.Chrome(executable_path='./chromedriver')
I get the following error instead of the previous one:
Traceback (most recent call last):
File "scraper.py", line 4, in <module>
driver = webdriver.Chrome(executable_path='./chromedriver')
File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__
desired_capabilities=desired_capabilities)
File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/home/joseph/.local/lib/python2.7/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: exited abnormally
(Driver info: chromedriver=2.36.540471 (9c759b81a907e70363c6312294d30b6ccccc2752),platform=Linux 4.4.0-43-Microsoft x86_64)
While working with Selenium v3.11.0, ChromeDriver v2.36 and Chrome v64.x you have to download the latest ChromeDriver from the ChromeDriver - WebDriver for Chrome and place it within your system. Next while initializing the WebDriver and the WebBrowser you have to mention the absolute path of the ChromeDriver as follows :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.quit()
Note : At the end of your Test Execution instead of close() invoke the quit() method so the WebDriver and WebBrowser both are destroyed.
You may need to try various versions of the driver depending on version of chrome. Also, make sure your path is correct and pointing to executable driver file(.exe)
driver = webdriver.Chrome(executable_path='./chromedriver/chromedriver.exe')

about selenium, not knowing what errors are

Here's my code and the error Python provides.
I'm a beginner using Python 3.6. Can anyone helps find out what's wrong? Many thanks.
#!/usr/bin/env python
#coding: utf-8
from selenium import webdriver
driver = webdriver.Chrome('C:\\Users\\Admin\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe')
browser = webdriver.Chrome()
browser.get('http://www.bing.com/')
Traceback:
Traceback (most recent call last):
File "C:\Users\Admin\Desktop\cxy61.com - html\python spider\001 selenium import.py", line 8, in <module>
driver = webdriver.Chrome('C:\\Users\\Admin\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe')
File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
desired_capabilities=desired_capabilities)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 140, in __init__
self.start_session(desired_capabilities, browser_profile)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 229, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 297, in execute
self.error_handler.check_response(response)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: session not created exception: Chrome version must be >= 58.0.3029.0
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 10.0.14393 x86_64)
You have to take care of a couple of things as follows:
As you are using chromedriver v2.31 the error clearly states Chrome version must be >= 58.0.3029.0. So you need to bump up your Google Chrome version to v58.0 or above.
If you want to specify the absolute path of the chromedriver within single quotes '...' then you have to provide single forward slashes \ only.
If you are assigning the webdriver instance to the driver, you have to use the driver instance only to open any url
Here is your own code with the above mentioned changes:
from selenium import webdriver
driver = webdriver.Chrome(r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('http://www.bing.com/')

[Python][Selenium]Unable to use webdriver with Chrome

I'm a beginner in Python and Selenium, and I dont know what is the error in my code or environment...
# encoding: utf-8
import time
from selenium import webdriver
from bs4 import BeautifulSoup
driver = webdriver.Chrome(executable_path=r'C:/Python27/Scripts/chromedriver')
time.sleep(3)
driver.get('https://www.google.com.tw/')
for i in range(10):
driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
time.sleep(1)
print ("Scrolling...")
driver.close()
And here is error messages...
C:\Python27\python.exe D:/PythonPratice/test.py
Traceback (most recent call last):
File "D:/PythonPratice/test.py", line 9, in
driver.get('https://www.google.com.tw/')
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 250, in get
self.execute(Command.GET, {'url': url})
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Runtime.executionContextCreated has invalid 'context': {"auxData":{"frameId":"2172.1","isDefault":true},"id":1,"name":"","origin":"://"}
(Session info: chrome=56.0.2924.87)
(Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86_64)
Process finished with exit code 1
Much thanks!
Try to download latest version of chromedriver and put it to C:/Python27/Scripts/ instead of outdated one
you should provide full path of chromedriver.exe, see the following:
driver = webdriver.Chrome(executable_path=r'C:/Python27/Scripts/chromedriver.exe')

Categories

Resources