Selenium Chromedriver Won't Start - python

I'm trying to test my code using selenium with chrome driver. But I always got this error message in my terminal.
======================================================================
ERROR: test_input_status (myweb.tests.StorySixFunctionalTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/fredypasaud/Documents/PPW/story_six/myweb/tests.py", line 58, in setUp
self.selenium = webdriver.Chrome('./chromedriver',chrome_options = chrome_options)
File "/home/fredypasaud/Documents/PPW/django01/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/home/fredypasaud/Documents/PPW/django01/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/fredypasaud/Documents/PPW/django01/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/fredypasaud/Documents/PPW/django01/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/fredypasaud/Documents/PPW/django01/lib/python3.6/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 normally
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /snap/bin/chromium is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
And i put my chromedriver in the same directory as the manage.py of my project. And here's some code about from my test.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
class StorySixFunctionalTest(TestCase):
def setUp(self):
chrome_options = Options()
self.selenium = webdriver.Chrome('./chromedriver',chrome_options = chrome_options)
chrome_options.add_argument('--dns-prefetch-disable')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('disable-gpu')
chrome_options.addArguments("--disable-extensions");
chrome_options.addArguments("--disable-dev-shm-usage");
super(StorySixFunctionalTest, self).setUp()
def tearDown(self):
self.selenium.quit()
super(StorySixFunctionalTest, self).tearDown()
def test_input_status(self):
selenium = self.selenium
selenium.get('http://127.0.0.1:8000/index/')
status = selenium.find_element_by_id('id_status')
submit = selenium.find_element_by_id('submit')
status.send_keys("Coba Coba")
submit.send_keys(Keys.RETURN)
Update :
I'm trying to debug my program by showing the log of chromedriver, and i got this error instead (which is weird because i have canbera module installed)
Gtk-Message: 14:40:58.177: Failed to load module "canberra-gtk-module"
Gtk-Message: 14:40:58.196: Failed to load module "canberra-gtk-module"

Related

Firefox fails to open url when using webdriver python API

I am new to python and trying to build a selenium code to open a website via firefox.
I am using a debian stretch machine for the tests. The versions of the tools are given below :
geckodriver 0.24.0 ( 2019-01-28) , Python 2.7.13 , Mozilla Firefox 52.7.3 , selenium (3.141.0)
I see that the firefox window opens( even though in headless). But it doesnt proceed further with opening the website. The firefox instance waits for sometime after opening and then stops, the script gives error like
WebHandle.open_application("Firefox" , "http://www.google.com")
File "/home/yyyyy/yyyyy/yyyyy/yyyyy/script.py", line 49, in open_application
driver = webdriver.Firefox(firefox_options = options, executable_path='/usr/local/bin/geckodriver')
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 174, in init
keep_alive=True)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in init
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused
Code :
from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options as FirefoxOptions
options = FirefoxOptions()
options.add_argument("--headless")
print (" * Opening firefox session")
driver = webdriver.Firefox(firefox_options = options, executable_path='/usr/local/bin/geckodriver')
driver.get("https://www.google.com")
driver.maximize_window()
print(driver.title)
While looking into the geckodriver.log , I see the following errors
1624193615192 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "--headless" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofile.Wwi0327B9dAL"
1624193616996 Marionette INFO Listening on port 2828
[Child 28357] ###!!! ABORT: Aborting on channel error.: file /build/firefox-esr-52.7.3esr/ipc/glue/MessageChannel.cpp, line 2152
[Child 28357] ###!!! ABORT: Aborting on channel error.: file /build/firefox-esr-52.7.3esr/ipc/glue/MessageChannel.cpp, line 2152
###!!! [Child][MessageChannel] Error: (msgtype=0x3E0003,name=PCompositable::Msg_Destroy) Channel error: cannot send/recv
###!!! [Child][MessageChannel] Error: (msgtype=0x3E0003,name=PCompositable::Msg_Destroy) Channel error: cannot send/recv
Thanks already for any help in fixing the issue
Try placing the geckodriver.exe file in a separate folder & drive and updating the path in executable_path.
eg:
driver = webdriver.Firefox(firefox_options = options, executable_path=r"D:/Python/drivers/geckodriver.exe")

selenium.common.exceptions.WebDriverException:unknown error:cannot connect to chrome at 127.0.0.1:9222 from unknownerror:unable to discover open pages

I managed to connect selenium driver to an already opened chrome session with the following method in python
def iniciar_google_previa():
os.system('google-chrome --no-sandbox --remote-debugging-port=9222 --user-data-dir="/home/natanael/.config/google-chrome/Default" &')
time.sleep(2)
options1 = Options()
options1.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_driver = "/usr/local/bin/chromedriver"
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options1)
return driver
And it worked really good, then I can open any site with driver.get("site_url") to start scraping.
I wanted to try the same with docker containers. Installed all the requierements for the dockerfile, built it and then tried to run it but I get the following error
File "final1.py", line 104, in <module>
iniciar_ciclo()
File "final1.py", line 21, in iniciar_ciclo
driver = iniciar_google_previa()
File "final1.py", line 67, in iniciar_google_previa
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options1)
File "/opt/app-root/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/opt/app-root/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/opt/app-root/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/opt/app-root/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/opt/app-root/lib/python3.6/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 connect to chrome at 127.0.0.1:9222
from unknown error: unable to discover open pages
I thought it was because it was trying to run a GUI so I added
--headless
google-chrome --no-sandbox --headless --remote-debugging-port=9222 --user-data-dir="/home/natanael/.config/google-chrome/Default"
but got the same error.
already tried installing
xvfb
and then
xvfb-run google-chrome --no-sandbox --remote-debugging-port=9222 --user-data-dir="/home/natanael/.config/google-chrome/Default"
which gave the output
DevTools listening on ws://127.0.0.1:9222/devtools/browser/52bbc92b-e096-4897-8661-233ee573edaf
apparentley it executes the chrome session but is still unable to connect to it and I don't know what else to try

Selenium error in python 3 || lib\subprocess.py [duplicate]

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")

Whenever I try to run Chrome using selenium using Python I get this error. has anyone experienced this before?

Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path = r"C:\Users\Dell\Desktop\chromedriver.exe")
driver.get("http:/https://stackoverflow.com/")
Traceback (most recent call last):
File "C:\Users\Dell\Desktop\Intro.py", line 3, in
driver = webdriver.Chrome(executable_path = r"C:\Users\Dell\Desktop\chromedriver.exe")
File "C:\Users\Dell\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 76, in init
RemoteWebDriver.init(
File "C:\Users\Dell\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\Dell\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\Dell\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\Dell\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.WebDriverException: Message: unknown error: failed to write prefs file
This can cause if your C drive has not enough space. So use any tools like Disk Cleaner or CCleaner to free up some space.
Here is my version of the working code.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path="C:\Windows\chromedriver.exe")
driver.get("https://stackoverflow.com/")
Try adding chromedriver to C:\Windows.
Check both chrome and chromedriver having the same version.

Chrome Driver Options to Open Default Browser and not a new Window Python

I am Trying to Open the chrome in a configuration where all the extensions as well as in logged-in state.
I came across an answer to remove default from the chrome driver Options Module. However, when I added this and tried to execute the program runs Opens a new window with everything enabled, but it does not proceed to open the URL specified and stays at starting page.
Here is my code:
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
options=Options()
options.add_argument("user-data-dir=C:\\Users\\aman krishna\\AppData\\Local\\Google\\Chrome\\User Data\\")
driver=webdriver.Chrome("C:\\Users\\aman krishna\Desktop\\New folder (3)\chromedriver.exe",chrome_options=options)
driver.get("https://www.google.com")
In this code a new window opens but does not search for www.google.com
After some time an error message is displayed i.e
Traceback (most recent call last):
File "C:/Users/aman krishna/PycharmProjects/untitled9/beautifoulsoup.py", line 5, in <module>
driver=webdriver.Chrome("C:\\Users\\aman krishna\Desktop\\New folder (3)\chromedriver.exe",chrome_options=options)
File "C:\Users\aman krishna\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\chrome\webdriver.py", line 75, in __init__
desired_capabilities=desired_capabilities)
File "C:\Users\aman krishna\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\webdriver.py", line 154, in __init__
self.start_session(desired_capabilities, browser_profile)
File "C:\Users\aman krishna\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\webdriver.py", line 243, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\aman krishna\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "C:\Users\aman krishna\AppData\Roaming\Python\Python36\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
(Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 6.3.9600 x86_64)

Categories