Unable to Open Chrome WebDriver with Selenium for Python - 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')

Related

Python Selenium Error: Cannot find Opera binary

i'm trying out Python Selenium with my main browser, Opera, but i get a massive error when i execute the script, here's the python script:
from selenium import webdriver
path = r"C:\Users\sleep\Programs\operadriver_win64\operadriver.exe"
driver = webdriver.Opera(executable_path=path)
driver.get('https://youtube.com')
And here's the error:
Traceback (most recent call last):
File "C:\Users\sleep\Programs\Status-Entrega\main.py", line 6, in <module>
driver = webdriver.Opera(executable_path=r'C:\Users\sleep\Programs\operadriver_win64\operadriver.exe')
File "C:\Users\sleep\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\opera\webdriver.py", line 79, in __init__
OperaDriver.__init__(self, executable_path=executable_path,
File "C:\Users\sleep\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\opera\webdriver.py", line 55, in __init__
ChromiumDriver.__init__(self,
File "C:\Users\sleep\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "C:\Users\sleep\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\sleep\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\sleep\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\sleep\AppData\Local\Programs\Python\Python39\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 find Opera binary
(Driver info: operadriver=91.0.4472.77 (1cecd5c8a856bc2a5adda436e7b84d8d21b339b6-refs/branch-heads/4472#{#1246}),platform=Windows NT 10.0.19042 x86_64)
How can i fix this?
can you try this :
from selenium import webdriver
from selenium.webdriver.opera.options import Options
options = Options()
options.binary_location = r'C:\opera.exe path'
driver = webdriver.Opera(opera_options = options,
executable_path=r'C:\operadriver.exe path')
it might work, don't forget one is the opera path and one is the operadriver path
This link may help: https://github.com/operasoftware/operachromiumdriver/blob/master/examples/desktop.py
import time
from selenium import webdriver
from selenium.webdriver.chrome import service
webdriver_service = service.Service('path/to/operadriver')
webdriver_service.start()
driver = webdriver.Remote(webdriver_service.service_url, webdriver.DesiredCapabilities.OPERA)
driver.get('https://www.google.com/')
input_txt = driver.find_element_by_name('q')
input_txt.send_keys('operadriver\n')
time.sleep(5) #see the result
driver.quit()

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.common.exceptions.WebDriverException: Message: unknown error: Failed to create a Chrome process

I am working on a web application, and was attempting to run a basic test script with seleium, just to make sure my code was working
from selenium import webdriver
import time
import os
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
options.binary_location = os.getcwd()
driver = webdriver.Chrome(chrome_options=options, executable_path=r'./chromedriver')
driver.get('http://codepad.org')
I have the chromedriver in the current directory, and I think I am using the correct version of the chromedriver (75.0.3770.90). The full error I am getting is:
Traceback (most recent call last):
File "test.py", line 9, in <module>
driver = webdriver.Chrome(options=options, executable_path=r'./chromedriver')
File "/Users/kylerood/Documents/summer19/makeFriends/env/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/Users/kylerood/Documents/summer19/makeFriends/env/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/Users/kylerood/Documents/summer19/makeFriends/env/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/Users/kylerood/Documents/summer19/makeFriends/env/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/kylerood/Documents/summer19/makeFriends/env/lib/python3.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: Failed to create a Chrome process.
If anyone has any insight into a fix I could try that would be greatly appreciated!
Remove/comment the below line.
options.binary_location = os.getcwd()
As the binary is not located in the current working directory you are getting this error message.
binary_location is the location where your chrome.exe is located.
And make sure you have the chromedriver in the same folder you have this test located. Otherwise your script will fail with below error message.
selenium.common.exceptions.WebDriverException: Message: 'chromedriver` executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
The value binary_location must be an .exe file which specify the chrome.exe you want to run.
So this code works for me:
chrome_options = Options()
chrome_options.binary_location = '****\\chrome.exe'
driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver\\chromedriver.exe"), options=chrome_options)
Hope it helps you

selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH error with Headless Chrome

when i run my script , i got this error
Traceback (most recent call last):
File "C:\Users\ishaq\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\Users\ishaq\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\Users\ishaq\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 992, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/ishaq/AppData/Local/Programs/Python/Python36/headless.py", line 9, in <module>
driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver"), chrome_options=chrome_options)
File "C:\Users\ishaq\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 62, in __init__
self.service.start()
File "C:\Users\ishaq\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
here is my script
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.binary_location =
r'C:\Users\ishaq\Desktop\chrome\chromedriver.exe'
driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver"),
chrome_options=chrome_options)
driver.get("http://www.duo.com")
magnifying_glass = driver.find_element_by_id("js-open-icon")
if magnifying_glass.is_displayed():
magnifying_glass.click()
else:
menu_button = driver.find_element_by_css_selector(".menu-trigger.local")
menu_button.click()
search_field = driver.find_element_by_id("site-search")
search_field.clear()
search_field.send_keys("Olabode")
search_field.send_keys(Keys.RETURN)
assert "Looking Back at Android Security in 2016" in driver.page_source
driver.close()
If we analyze the logs it seems the main issue is with in start os.path.basename(self.path) and subsequent error message:
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH
So it's clear from the error that the Python client was unable to locate the chromedriver executable binary.
You need to take care of a couple of things as follows:
chrome_options.binary_location : The parameter points to the chrome.exe not the chromedriver.exe
os.path.abspath("chromedriver") will pick up the file path of chromedriver but won't append chromedriver.exe at the end.
Here is the sample code on my windows-8 system to start google-chrome-headless:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("http://www.duo.com")
print("Chrome Browser Initialized in Headless Mode")
driver.quit()
print("Driver Exited")

First selenium program not working

I've just loaded Python Selenium into my Ubuntu system and I'm following the Getting Started tutorial on ReadTheDocs. When I run this program:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
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()
But I'm getting this error:
Traceback (most recent call last):
File "/home/henry/Documents/Scraper/test-selenium.py", line 4, in <module> driver = webdriver.Firefox()
File "/usr/local/lib/python2.7/distpackages/selenium/webdriver/firefox/webdriver.py", line 80, in __init__self.binary, timeout)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 52, in __init__self.binary.launch_browser(self.profile, timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 68, in launch_browser self._wait_until_connectable(timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 108, in _wait_until_connectable % (self.profile.path))
WebDriverException: Message: Can't load the profile. Profile Dir: /tmp/tmp8xr2V3 If you specified a log_file in the FirefoxBinary constructor, check it for details.
I think you are using selenium 2.53.6
The issue is compatibility of firefox with selenium, since firefox>= 48 need Gecko Driver(https://github.com/mozilla/geckodriver) to run testcases on firefox. or you can downgrade the firefox to 46..from this link https://ftp.mozilla.org/pub/firefox/releases/46.0.1/
Looks like there's a problem with the webdriver - do you have Firefox installed on your RasPi?
(If not, this might help: https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=150438)

Categories

Resources