Python selenium webdriver "Session not created" exception when opening Chrome - python-3.x

Here's my code for opening Chrome:
from selenium import webdriver
driver=webdriver.Chrome('C:\\Users\\Imran\\AppData\\Local\\Programs\\Python\\Python36\\selenium\\chromedriver.exe')
Error when running program:
Traceback (most recent call last):
File "C:/Users/Imran/PycharmProjects/webscraping/WF Item.py", line 6, in <module>
driver = webdriver.Chrome('C:\\Users\\Imran\\AppData\\Local\\Programs\\Python\\Python36\\selenium\\chromedriver.exe')
File "C:\Users\Imran\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
desired_capabilities=desired_capabilities)
File "C:\Users\Imran\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
File "C:\Users\Imran\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
File "C:\Users\Imran\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Users\Imran\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: session not created exception
from unknown error: Runtime.executionContextCreated has invalid 'context': {"auxData":{"frameId":"27028.1","isDefault":true},"id":1,"name":"","origin":"://"}
(Session info: chrome=54.0.2840.71)
(Driver info: chromedriver=2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129),platform=Windows NT 6.3.9600 x86_64)
Getting the following exception:
selenium.common.exceptions.WebDriverException: Message: session not created exception

Update your chromedriver to 2.24 version from the given url:
http://chromedriver.storage.googleapis.com/index.html?path=2.24/
That worked for me.

Setting LANG=en_US.UTF-8 before executing my script works for me.
See this: https://bugs.chromium.org/p/chromedriver/issues/detail?id=1552#c43
In Python:
import os
os.environ["LANG"] = "en_US.UTF-8"

To install the chromedriver on windows, download and unzip the chromedriver_win32.zip and add the path to the folder containing the chromedriver.exe in the environment variables setting [System properties - Advanced system settings - Environment variables].

Related

getting "selenium.common.exceptions.SessionNotCreatedExceptionSelenium" error using InstaPy

[SETUP:
Windows 10 x64
PyCharm 2019.3.3 (Community Edition)]
I'm trying to create a simple Insta bot with InstaPy but I keep running into the same error
CODE:
from instapy import InstaPy
InstaPy(username='username', password='password').login()
I keep getting the following error:
Traceback (most recent call last):
File "C:/Users/Niku/PycharmProjects/InstaBot/InstaBot.py", line 6, in <module>
InstaPy(username='Rubixmusicprod', password='Niku2020').login()
File "C:\Users\Niku\PycharmProjects\InstaBot\venv\lib\site-packages\instapy\instapy.py", line 309, in __init__
self.browser, err_msg = set_selenium_local_session(
File "C:\Users\Niku\PycharmProjects\InstaBot\venv\lib\site-packages\instapy\browser.py", line 111, in set_selenium_local_session
browser = webdriver.Firefox(
File "C:\Users\Niku\PycharmProjects\InstaBot\venv\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 170, in __init__
RemoteWebDriver.__init__(
File "C:\Users\Niku\PycharmProjects\InstaBot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\Niku\PycharmProjects\InstaBot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\Niku\PycharmProjects\InstaBot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Niku\PycharmProjects\InstaBot\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities
I've tried upgrading selenium, along with Geckodriver and related drivers.
It was a mission just to get InstaPy installed!
I may just have to go the browser automation way instead here.
Any help would be greatly appreciated!!
You must use Selenium with Geckodrive in Firefox
session = InstaPy(username=INSTAGRAM_LOGIN,
password=INSTAGRAM_PASSWORD,
geckodriver_path='geckodriver.exe')

Executing script with Selenium inside <iframe> doesn't work, but when doing in Console it does

I'm automating a service with Selenium, and the website I need to access uses iframes tags, and I need to alter the value of some elements inside this tag.
I was using the Edge WebDriver, but I changed to Chrome because I thought it'd solve the problem, and unfortunately, it didn't, so I don't think it had anything to do with the Driver, but I'd rather keep with Chrome, if possible.
This is the line that simulates the script:
gc.execute_script("""document.evaluate('//*[#id="QRYSELECT_WRK_QRYSEARCHTEXT254"]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.setAttribute('value','foo')""")
And it raises me an exception about null pointers, but basically, it's saying that the browser couldn't find this element, and, therefore, couldn't change its value.
Ironically, when I open the console and write the following command (which is the same script above):
document.evaluate('//*[#id="QRYSELECT_WRK_QRYSEARCHTEXT254"]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.setAttribute('value','foo')
It works.
Here's the traceback to the error when I call execute_script from gc:
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
gc.execute_script("""document.evaluate('//*[#id="QRYSELECT_WRK_QRYSEARCHTEXT254"]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.setAttribute('value','CEL_ADMITIDOS')""")
File "C:\Program Files\Python36\lib\site-packages\selenium\webdriver\support\event_firing_webdriver.py", line 88, in execute_script
return self._dispatch("execute_script", (script, self._driver), "execute_script", unwrapped_args)
File "C:\Program Files\Python36\lib\site-packages\selenium\webdriver\support\event_firing_webdriver.py", line 160, in _dispatch
raise e
File "C:\Program Files\Python36\lib\site-packages\selenium\webdriver\support\event_firing_webdriver.py", line 157, in _dispatch
result = getattr(self._driver, d_call)(*d_args)
File "C:\Program Files\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 636, in execute_script
'args': converted_args})['value']
File "C:\Program Files\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Program Files\Python36\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: Cannot read property 'setAttribute' of null
(Session info: chrome=71.0.3578.98)
(Driver info: chromedriver=71.0.3578.137 (86ee722808adfe9e3c92e6e8ea746ade08423c7e),platform=Windows NT 10.0.17134 x86_64)
So I guess it's probably something related to permissions...? But I don't know, it's quite weird to get two different results to the same script, isn't it?
Thanks!

Xvfb Selenium headless Chrome on Linux in Python

I'm trying to run chrome headless with selenium in Python.
I tried xvfb-run -a python3 message.py
And i get this error:
Traceback (most recent call last):
File "message.py", line 388, in <module>
initialize()
File "message.py", line 54, in initialize
driver = webdriver.Chrome(chrome_options = chrome_options)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__
desired_capabilities=desired_capabilities)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 237, 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.33.506178 (69ae10f91723897591ef1a3b465aa5d35011eb5e),platform=Linux 3.13.0-96-generic x86)
And initialize():
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--verbose")
chrome_options.add_argument("--log-path=chrome.log")
chrome_options.add_argument("window-size=1200x600")
chrome_options.add_argument("user-data-dir=\\")
driver = webdriver.Chrome(chrome_options = chrome_options)
driver.get('http://google.com')
I'm using Python 3.5 and chromedriver 2.33.506178 for Linux32 (Ubuntu as Server without Desktop)
I tested the same code on my other pc with linux mint, all works fine.
Hope you can help me :)
After checking my actual rights of writing/reading/executing I found my mistake. chromedriver weren't able to load while trying to execute with root rights.
After I changed chromedrivers permission all works fine now.

Selenium: NoSuchFrameException for Chrome Extension

I have a Chrome extension and I'd like to test it with Selenium. The extension places an iframe tag to pages.
I can successfully get the iframe tag by running the following command:
top_frame = driver.find_element(By.XPATH, value='//iframe[#class="top-frame"]')
However, when I try switching to the frame I get 'NoSuchFrameException' error.
driver.switch_to.frame(top_frame)
The following line outputs the frame's class name correctly
print(top_frame.get_attribute('class'))
Error log:
Testing http://www.google.com
Traceback (most recent call last):
File "test/run_tests.py", line 200, in <module>
main()
File "test/run_tests.py", line 197, in main
run_tests(args.plugin, args.out, args.url)
File "test/run_tests.py", line 167, in run_tests
button = check_button(driver, url)
File "test/run_tests.py", line 84, in check_button
driver.switch_to.frame(top_frame)
File "/home/burak/.virtualenv/extension/lib/python3.5/site-packages/selenium/webdriver/remote/switch_to.py", line 87, in frame
self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference})
File "/home/burak/.virtualenv/extension/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/home/burak/.virtualenv/extension/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchFrameException: Message: no such frame: element is not a frame
(Session info: chrome=60.0.3112.90)
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Linux 4.10.0-30-generic x86_64)
Try this man,
driver.switch_to.frame(driver.find_element(By.XPATH,value=".//iframe[#src='give your frame source name here..']"));
In the latest version of Chrome, most likely there is a bug that breaks the switch_to_frame() call. Therefore, I started using Firefox instead, everything is smooth for now.
This link is to the associated issue report on Chromium. It has not been resolved yet. (25/10/17)
https://bugs.chromium.org/p/chromedriver/issues/detail?id=2019

Error while open browser from selenium

my script
from selenium import webdriver
o = webdriver.Firefox()
and i get this problem
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 142, in __init__
self.service.start()
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "/usr/lib/python3.5/subprocess.py", line 676, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py", line 1283, in _execute_child
raise child_exception_type(errno_num, err_msg)
OSError: [Errno 8] Exec format error
i searched on it and i found this
OSError: [Errno 8] Exec format error selenium
i get chromedriver and put it in '/usr/local/bin/'
like supposed,
it doesn't help
and also did the same thing with geckodriver
Trouble setting up selenium with python3(linux)
nothing work same error
and i try this shot
#!usr/bin/python
#!/usr/bin/env bash
from selenium import webdriver
web = webdriver.Firefox(executable_path="/usr/local/bin/chromedriver")
it give me that error :
Traceback (most recent call last):
File "/root/Desktop/test.py", line 5, in <module>
web = webdriver.Firefox(executable_path="/usr/local/bin/chromedriver")
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 142, in __init__
self.service.start()
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/service.py", line 96, in start
self.assert_process_still_running()
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/service.py", line 109, in assert_process_still_running
% (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service /usr/local/bin/chromedriver unexpectedly exited. Status code was: 1
Note :
operation system is kali linux
SMP Debian 4.9.18-1kali1 version,
firefox and selenium is up to date
driver should be updated one & did u try webdriver.Firefox(driver_path) ?
Uninstall instance of webdriver dependency and install the correct one which corresponds to your OS and browser version.Also when you give path of chrome ,it should end with chrome.exe and not chrome.

Resources