How can I handle selenium and geckodriver install error? - django

I ran the following code by installing selenium and django module.
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://localhost:8000')
assert 'Django' in browser.title
For the selenium module, I need geckodriver for firefox browser.
So, I installed geckodriver by different ways - 1. npm, 2. brew, 3. direct install (download from here and move it to /usr/local/bin or /usr/bin. All the ways did not work for the above test code.
I got the following error message:
Traceback (most recent call last):
File "functional_tests.py", line 3, in <module>
browser.get('http://localhost:8000')
File "/Users/kiyeonj/opt/anaconda3/envs/tdd_practice/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
self.execute(Command.GET, {'url': url})
File "/Users/kiyeonj/opt/anaconda3/envs/tdd_practice/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/kiyeonj/opt/anaconda3/envs/tdd_practice/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: Reached error page: about:neterror?e=connectionFailure&u=http%3A//localhost%3A8000/&c=UTF-8&d=Firefox%20can%E2%80%99t%20establish%20a%20connection%20to%20the%20server%20at%20localhost%3A8000.
Please let me know what the problem is..

I think you need a gecko driver as well.
You can download from here
and put it into a folder, preferably where you have your .py files
and then use it like this :-
from selenium import webdriver
driver = webdriver.Firefox(executable_path = "D:\geckodriver.exe")
driver.maximize_window()
browser.get('http://localhost:8000')
assert 'Django' in browser.title
PS :
executable_path = 'full file path to your gecko driver'

Related

Chromedriver crashes with "unrecognized capability: chromeOptions"

I have been using a simple Python2.7 script using Selenium with Chromedriver to open a browser and download some files. Lately, I got an error saying that Chromedriver is outdated (Chrome version must be between 70 and 73) while my Chrome was ver. 75. So I downloaded ChromeDriver 75.0.3770.90 which should be OK for my Chrome version, but I got another error saying "selenium.common.exceptions.WebDriverException: Message: invalid argument: unrecognized capability: chromeOptions".
I have found some tips on the Internet saying that a way ChromeOptions should be used had changed, but I am unable to find how these changes exactly should look like in Python code. I'll be grateful for any helpful hint or solution.
def browser(full_url):
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
options = Options()
options.add_experimental_option("prefs", {
"download.default_directory": r"C:\Users\%s\AppData\Local\Temp\\" % getpass.getuser(),
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
options.add_argument("--window-size=640,480")
driver = webdriver.Chrome(chrome_options=options)
try:
driver.get(full_url)
print("\nURL successfully Accessed\n")
time.sleep(9)
finally:
driver.close()
browser(full_url)
Error I got:
Traceback (most recent call last):
File "C:\Users\M\Documents\Skrypty\domains.py", line 59, in <module>
browser(full_url)
File "C:\Users\M\Documents\Skrypty\domains.py", line 46, in browser
driver = webdriver.Chrome(chrome_options=options)
File "C:\Python27\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
desired_capabilities=desired_capabilities)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 98, in __init__
self.start_session(desired_capabilities, browser_profile)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 188, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: invalid argument: unrecognized capability: chromeOptions
Just me guessing here but I encountered this as well. I used Python 2.7 and Selenium 3.4.1. I couldn't replicate the problem with Python 3.7 and with Selenium 3.14.0. So the changes lie probably in the changed Selenium. You should look into that.
EDIT: Yeah I just updated Selenium (to 3.14.0) and the problem went away. Now I got the driver working with both Python 2.7 and 3.7

WebDriverException() in Python2.7 using xvfb-run for chrome on Amazon Linux AMI Red-hat 7.2

Trying to run selenium for chrome through
/usr/bin/xvfb-run -a -e /tmp/selenium.log -w 0 /usr/bin/python2.7
and getting error like
(<class 'selenium.common.exceptions.WebDriverException'>, WebDriverException(), <traceback object at 0x7fc7cfc9c128>)
the strange thing is i am not even getting complete stack trace and the -e option for xvfb-run also is not returning any errors in file .
Here is my code
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
f = webdriver.ChromeOptions()
browser =webdriver.Chrome(executable_path='/home/local/chromedriver',chrome_options=f)
browser.get(url)
browser.quit()
i also tried with
executable_path='/usr/bin/chromedriver' and without executable_path
but getting the same error .
it was initially working with python 2.6 with firefox .
i checked maybe around 10-15 + similar questions even about how to enable more trace or debugging but none of the solutions seems to be doing the trick for me here.
selenium version 3.141.0
Edit :
i updated some code to be able to get complete stack trace and i got this error ,
WebDriverException: Message: The browser appears to have exited before
we could connect.
a bit more digging and saw its related to selenium/browser
installed old selenium version 2.53
thus above error was gone but now i am getting connection reset by peer error for everything , below is stack
(<class 'socket.error'>, error(104, 'Connection reset by peer'), <traceback object at 0x1e1e320>)
Traceback (most recent call last):
File "/var/www/Goose/src/goose/network.py", line 137, in get_html
browser.get(url)
File "/var/www/Goose/src/selenium/webdriver/remote/webdriver.py", line 245, in get
self.execute(Command.GET, {'url': url})
File "/var/www/Goose/src/selenium/webdriver/remote/webdriver.py", line 231, in execute
response = self.command_executor.execute(driver_command, params)
File "/var/www/Goose/src/selenium/webdriver/remote/remote_connection.py", line 395, in execute
return self._request(command_info[0], url, body=data)
File "/var/www/Goose/src/selenium/webdriver/remote/remote_connection.py", line 426, in _request
resp = self._conn.getresponse()
File "/usr/lib64/python2.6/httplib.py", line 1049, in getresponse
response.begin()
File "/usr/lib64/python2.6/httplib.py", line 433, in begin
version, status, reason = self._read_status()
File "/usr/lib64/python2.6/httplib.py", line 389, in _read_status
line = self.fp.readline(_MAXLINE + 1)
File "/usr/lib64/python2.6/socket.py", line 479, in readline
data = self._sock.recv(self._rbufsize)
error: [Errno 104] Connection reset by peer
even requests [security] is installed as i had seen one cause to be that .
solved it ,
the issue was the chrome browser and drivers were not properly installed , and their versions not compatible ,
installed latest chrome to run on the ami server using a script i got from
https://intoli.com/blog/installing-google-chrome-on-centos/
installed perfectly ,
added few key arguments ,
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('path to driver',chrome_options=chrome_options)
and viola it started working ,

Message: 'geckodriver' executable needs to be in PATH, but it already is?

I am writing a script that will save the complete contents of a web page. If I try using urllib2 and bs4 it only writes the contents of the logon page and none of the content after navigating to a search within the page. However, if I do a ctrl + s on the search results page, an html file is saved to disk that when opened in a text editor has all of the contents from the search results.
I've read several posts here on the subject and am trying to use the steps in this one:
How to save "complete webpage" not just basic html using Python
However, after installing geckodriver and setting the sys path variable I continue to get errors. Here is my limited code:
from selenium import webdriver
>>> from selenium.webdriver.common.action_chains import ActionChains
>>> from selenium.webdriver.common.keys import Keys
>>> br = webdriver.Firefox()
Here is the error:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 142, in __init__
self.service.start()
File "C:\Python27\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
os.path.basename(self.path), self.start_error_message)
WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
And here is where I set the sys path variable:
I've restarted after setting sys path variable.
UPDATE:
I am now trying to use the chromdriver as this seemed more straight forward. I downloaded hromedriver_win32.zip II'm on a windows laptop) from chromedriver's download page, set the environmetal variable path to:
C:\Python27\Lib\site-packages\selenium\webdriver\chrome\chromedriver.exe
but am getting the similar following error:
>>> br = webdriver.Chrome()
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python27\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 62, in __init__
self.service.start()
File "C:\Python27\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
os.path.basename(self.path), self.start_error_message)
WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
You have also to add the path of Firefox to the system variables manually,
you maybe have installed firefox some other location while Selenium is trying to find firefox and launch from default location but it couldn't find. You need to provide explicitly firefox installed binary location:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('path/to/installed firefox binary')
browser = webdriver.Firefox(firefox_binary=binary)
browser = webdriver.Firefox()

ImportError: No module named phonenumbers in Odoo (OpenERP v8)

I'm trying to intall the asterisk_click2dial module on ODOO and this error comes to me in the log file:
ValueError: Routing: posting a message without model should be with a parent_id (private mesage).
2015-03-09 15:23:38,262 11093 ERROR ? werkzeug: Error on request:
Traceback (most recent call last):
File "/home/odoo/odoo/lib/python2.7/site-packages/werkzeug/serving.py", line 177, in run_wsgi
execute(self.server.app)
File "/home/odoo/odoo/lib/python2.7/site-packages/werkzeug/serving.py", line 165, in execute
application_iter = app(environ, start_response)
File "/opt/odoo/openerp/service/server.py", line 281, in app
return self.app(e, s)
File "/opt/odoo/openerp/service/wsgi_server.py", line 216, in application
return application_unproxied(environ, start_response)
File "/opt/odoo/openerp/service/wsgi_server.py", line 202, in application_unproxied
result = handler(environ, start_response)
File "/opt/odoo/openerp/http.py", line 1274, in __call__
self.load_addons()
File "/opt/odoo/openerp/http.py", line 1293, in load_addons
m = __import__('openerp.addons.' + module)
File "/opt/odoo/openerp/modules/module.py", line 79, in load_module
mod = imp.load_module('openerp.addons.' + module_part, f, path, descr)
File "/opt/odoo/addons/base_phone/__init__.py", line 23, in <module>
from . import wizard
File "/opt/odoo/addons/base_phone/wizard/__init__.py", line 23, in <module>
from . import number_not_found
File "/opt/odoo/addons/base_phone/wizard/number_not_found.py", line 25, in <module>
import phonenumbers
ImportError: No module named phonenumbers
The problem is just that I installed that module (phonenumbers) plus the py-Asterisk module without errors using pip install phonenumbers and pip install py-Asterisk and the error persists.
I noticed I have at least two versions of python installed (2.6 and 2.7) but both modules are installed at the same version from odoo (I can see the modules in the python2.7 cli when, for example, I write phonenumbers or search).
Has anybody any idea what is happening to me? I'd be gratefull for some specific response. Thanks.
Here the connector's page: OpenERP - Asterisk connector
Ok, my bad. During odoo instalation I created a virtual environment under the Odoo system user account which was used solely by the Odoo server so I just need to install these modules under that enviorment. This works for me:
First let’s switch from root to the odoo user, then create a new virtual environment called odoo and activate it:
su - odoo
/usr/local/bin/virtualenv --python=/usr/local/bin/python2.7 odoo
source odoo/bin/activate
(If you have the virtual environment created like me just ignore the second line). Before starting the module installation we need to add the path to the PostgreSQL binaries, otherwise the PsycoPG2 module install will fail (I ignored this one too):
export PATH=/usr/pgsql-9.3/bin:$PATH
Then I can do pip install... Thank you all for your help.

Django - rest framework errors when python runserver

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/Library/Python/2.7/site-packages/dj_static.py", line 83, in __call__
return self.application(environ, start_response)
File "/Library/Python/2.7/site-packages/django/core/handlers/wsgi.py", line 255, in __call__
response = self.get_response(request)
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 178, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 220, in handle_uncaught_exception
if resolver.urlconf_module is None:
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 342, in urlconf_module
self._urlconf_module = import_module(self.urlconf_name)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/WillRedington/Desktop/Django Projects/propadev/propvocab/urls.py", line 3, in <module>
from rest_framework import routers
File "/Library/Python/2.7/site-packages/rest_framework/routers.py", line 23, in <module>
from rest_framework import views
File "/Library/Python/2.7/site-packages/rest_framework/views.py", line 11, in <module>
from rest_framework.compat import HttpResponseBase, View
File "/Library/Python/2.7/site-packages/rest_framework/compat.py", line 13, in <module>
from django.utils.six.moves.urllib import parse as urlparse
ImportError: No module named urllib
The server error message: [17/Dec/2014 16:26:45] "GET / HTTP/1.1" 500 59
This is checking if python returns the modules:
>>> import django.utils.six.moves
>>> import django.utils.six.moves.urllib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named urllib
>>>
My current python version is 2.7.2, Django is 1.5.1, rest framework is in 2.7 site-package.
I have already tried uninstalling and reinstalling Django several times and get the same error.
This is running on Mac OS X 10.9.5, please help me, I've been at this for 5 hours.
it seems like a dependency error. Maybe you downgraded your django, or randomly installed the rest framework package without controlling the dependencies.
django.utils.six.moves.urllib doesnt exist in django==1.5.1 yet. You can try to upgrade django to 1.5.7 for example. Then this import will work. Other things might still be broken though, I can´t tell from here. If you post the output of pip freeze here, it may be easier to help.
You aren´t working in a virtual environment, which is dangerous for dependencies. Best is to make a virtualenv, then install your packages with pip, taking care of dependencies.
Then run:
pip freeze > requirements.txt
and use that file in the future like this:
pip install -r requirements.txt
and edit the file accordingly if you install, update or remove packages.
Something that might be of interest is this: it generally never works to uninstall and reinstall the same package several times; if it doesn´t work once, it won´t work the second time either.
As someone mentioned before my response wasn't really an answer. I solved this error and potentially many more by installing a python virtual environment. To do this run the following
sudo pip install virtualenv
Then create a new folder for your virtual environments. cd into the created folder.
cd myvirtualenv
Then create a new virtual environment by running the following:
virtualenv venv
To run the virtual env:
source venv/bin/activate
This makes a seperate environment with it's own site packages, which is essential for any python developer, especially when working on multiple projects.
Source: http://docs.python-guide.org/en/latest/dev/virtualenvs/