how to Install xvfb and run with python and selenium? - python-2.7

I'm trying to use Xvfb to run headless browser. following process which I followed so far
1. Installed xvfb sudo apt-get install xvfb
2. Created virtualenv,
3. Installed xvfbwrapper
4. run following code
import unittest
from selenium import webdriver
from xvfbwrapper import Xvfb
class TestPages(unittest.TestCase):
def setUp(self):
self.xvfb = Xvfb(width=1280, height=720)
self.addCleanup(self.xvfb.stop)
self.xvfb.start()
self.browser = webdriver.Firefox()
self.addCleanup(self.browser.quit)
def testUbuntuHomepage(self):
self.browser.get('http://www.ubuntu.com')
self.assertIn('Ubuntu', self.browser.title)
def testGoogleHomepage(self):
self.browser.get('http://www.google.com')
self.assertIn('Google', self.browser.title)
if __name__ == '__main__':
unittest.main(verbosity=2)
But I'm getting following error, Even I tried installing this with sudo but no effect.
Traceback (most recent call last):
File "xvfbwrapper.py", line 4, in <module>
from xvfbwrapper import Xvfb
File "/home/ubuntu/unclescrooz/src/robinhood/xvfbwrapper.py", line 4, in <module>
from xvfbwrapper import Xvfb
ImportError: cannot import name Xvfb
Same issue with pyvirtualdisplay
Traceback (most recent call last):
File "pyvirtualdisplay.py", line 1, in <module>
from pyvirtualdisplay import Display
File "/m4k/projects/scrapper/stock/robinhood/pyvirtualdisplay.py", line 1, in <module>
from pyvirtualdisplay import Display
ImportError: cannot import name Display
With following code
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Firefox()
browser.get('http://www.google.com')
print browser.title
browser.quit()
display.stop()

Rename your files xvfbwrapper.py and pyvirtualdisplay.py to something else than the name of the module you import.

Related

flask_bcrypt, werkzeug.security.safe_str_cmp fails after upgrade to latest flask 2.1

I've upgraded my project to latest flask (2.1.2 and now I'm getting following error:
Traceback (most recent call last):
File "/home/ff/conveyors/run.py", line 1, in <module>
from conveyors import create_app
File "/home/ff/conveyors/conveyors/__init__.py", line 4, in <module>
from flask_bcrypt import Bcrypt
File "/home/ff/.cache/pypoetry/virtualenvs/conveyors-SV5d9Vx2-py3.9/lib/python3.9/site-packages/flask_bcrypt.py", line 21, in <module>
from werkzeug.security import safe_str_cmp
ImportError: cannot import name 'safe_str_cmp' from 'werkzeug.security'
In my __init__.py I have
...
from flask_bcrypt import bcrypt
from flask_login import LoginManager
...
and then below in create_app:
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(Config)
db.init_app(app)
bcrypt.init_app(app)
login_manager.init_app(app)
...
I've read Werkzeug release note about safe_str_cmp being removed from Werkzeug 2.1 and needs to be replaced with equivalent hmac functions, but I'm not sure what I need to do in my code to fix this.
Any help greatly appreciated.
Thanks.
It turned out to be a poetry update problem. Whatever I tried it wouldn't upgrade flask_bcrypt to the latest version 1.0.1 and stayed with 0.7.1
I had to start kind of from scratch and do
poetry add flask
...
for every package needed for this app

Instalation python2.7 in virtualenv

Some day I lost access to Trac which is Python2.7 software. (I suppose, that deprecation of Python2.x is clue). There is Python3 unstable version, but I can't use it, because there's no plugins yes, which are key for my Trac installation.
So created virtual environment:
virtualenv -p /usr/bin/python2.7 env
And finally installation is succesful!
pip install trac
But... Stil can't run it by wsgi apache2:
Traceback (most recent call last):
File "/var/lib/trac/apache/trac.wsgi", line 13, in <module>
import trac.web.main
File "/var/lib/trac/env/lib/python2.7/site-packages/trac/web/__init__.py", line 19, in <module>
from trac.web.api import *
File "/var/lib/trac/env/lib/python2.7/site-packages/trac/web/api.py", line 18, in <module>
from BaseHTTPServer import BaseHTTPRequestHandler
I tried to install pip install http but still the same problem
my wsgi file:
import sys
sys.stdout = sys.stderr
import os
os.environ['PKG_RESOURCES_CACHE_ZIP_MANIFESTS'] = '1'
os.environ['TRAC_ENV_PARENT_DIR'] = '/var/lib/trac'
os.environ['PYTHON_EGG_CACHE'] = '/var/lib/trac/eggs'
#import trac.db.postgres_backend
#trac.db.postgres_backend.PostgreSQLConnection.poolable = False
import trac.web.main
application = trac.web.main.dispatch_request
How to change wsgi or how to install missing module?

why does it say "module does not exist" despite multiple installations

I am running the following steps in Mac Terminal:
sudo easy_install pip
sudo pip install virtualenv
virtualenv NameOfFolder
cd NameOfFolder
source bin/activate
sudo pip install django
django-admin startproject NameOfFolderSub1
cd NameOfFolderSub1
python manage.py runserver
At this last steps, it communicates this msg occurred:
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 14, in <module>
import django
ModuleNotFoundError: No module named 'django'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 17, in <module>
"Couldn't import Django. Are you sure it's installed and "
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
Attempted to check the Django version using this command in Terminal:
python -m django --version
it confirmed that django is not there with the following msg:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3: No module named django
What did I do wrong int he step-by-step installation above ?
Appreciate the help.
The code in manage.py is as follows:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxx.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)
You've done sudo pip install django. Don't do that. You're in a virtualenv; just install directly without sudo; and use pip3 instead of pip.

Selenium Firefox webdriver for python KeyError 'value'

I started with the following basic python script for using selenium:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select, WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
path_bin='/usr/bin/firefox'
path_dr='/usr/local/bin/geckodriver'
profile = webdriver.FirefoxProfile()
binary=FirefoxBinary(path_bin)
self.driver = webdriver.Firefox(executable_path=path_dr,firefox_profile=profile,firefox_binary=binary)
self.driver.implicitly_wait(30)
Then, I tried executing it twice (first time without sudo and second time with sudo) as follows:
user4#pc-4:~/Scripts$ python test.py
Traceback (most recent call last):
File "test.py", line 2049, in <module>
self.driver = webdriver.Firefox(executable_path=path_dr,firefox_profile=profile,firefox_binary=binary)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 144, in __init__
self.service = Service(executable_path, log_path=log_path)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/service.py", line 44, in __init__
log_file = open(log_path, "a+") if log_path is not None and log_path != "" else None
IOError: [Errno 13] Permission denied: 'geckodriver.log'
user4#pc-4:~/Scripts$ sudo python test.py
Traceback (most recent call last):
File "test.py", line 2049, in <module>
self.driver = webdriver.Firefox(executable_path=path_dr,firefox_profile=profile,firefox_binary=binary)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 155, in __init__
keep_alive=True)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 183, in start_session
self.capabilities = response['value']
KeyError: 'value'
So, I have to understand the following:
As observed IOError: [Errno 13] Permission denied: 'geckodriver.log' is being observed when I run the program without a sudo. Is that really required?
Even though the browser window is opened, in spite of having resolved all the dependencies (I'm using Ubuntu 16.04) like geckodriver for Firefox, I'm getting the above error KeyError: 'value'. How can I resolve this? Do I have to make any changes in my code to avoid this?
I could use some helpful advice/tips/tweaks to get my program running.
P.S.: Digging into the web to learn on selenium, points to some architecture-specific issues as mentioned in here and here and none of them seems to have been solved and that concerns me. Would chrome be a better option?
Update: The following is the output of ls -alh:
-rw-r--r-- 1 root root 238K Aug 22 17:12 geckodriver.log
-rwxrwxrwx 1 user4 user 82K Aug 22 17:08 test.py
As you were trying to pass a couple of arguments when invoking webdriver.Firefox(), we need to consider that executable_path and firefox_binary both these arguments takes string type of arguments. So you need to pass the absolute path of geckodriver binary to executable_path and the absolute path of firefox binary to firefox_binary. Further you don't need to use binary=FirefoxBinary(path_bin). The following code block works fine on my Windows 8 machine:
Code is based on Python 3.6.1
from selenium import webdriver
path_bin=r'C:\Program Files\Mozilla Firefox\firefox.exe'
path_dr=r'C:\Utility\BrowserDrivers\geckodriver.exe'
profile = webdriver.FirefoxProfile()
driver = webdriver.Firefox(executable_path=path_dr,firefox_profile=profile,firefox_binary=path_bin)
driver.get('https://www.google.co.in')
As you are on ubuntu the following code block must work for you:
from selenium import webdriver
path_bin='/usr/bin/firefox'
path_dr='/usr/local/bin/geckodriver'
profile = webdriver.FirefoxProfile()
driver = webdriver.Firefox(executable_path=path_dr,firefox_profile=profile,firefox_binary=path_bin)
driver.get('https://www.google.co.in')
I had this same issue with
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 183, in start_session
self.capabilities = response['value']
KeyError: 'value'
So I have updated selenium and geckodriver to the latest version. This was the solution in my case.

No module named PXSSH

I am just a beginner using python 2.7 . Please forgive my naive- ness
my following code is not working
import sys
import os
import time
import re
import pxssh
a = ['192.168.50.11', '192.168.50.12']
for i in a:
Host = i
print Host
User = "xyz"
Password = "abc"
tn = pxssh.pxssh(Host)
tn.read_until("login as: ")
tn.write(User + "\n")
tn.read_until("Password: ")
tn.write(Password + "\n")
tn.read_until("#")
tn.write("show run\n")
time.sleep(3)
output = tn.read_all()
time.sleep(3)
f = open("C:/Python27/file/"+ Host + ".txt", "w+")
f.write(output)
f.close()
tn.close()
it gives me following error :
Traceback (most recent call last):
File "C:/Python27/backup_config_files", line 5, in <module>
import pxssh
ImportError: No module named pxssh
Please let me know how can i resolve this pxssh module issue with python 2.7 in windows
Install pexpect using
pip install pexpect
Then import using
from pexpect import pxssh
Refs: docs
This question is similar to this one.
pxssh is a class, that you must import from the module pexpect.
Your import would look like:
from some.location.pexpect import pxssh
If you do not have the pexpect module, pip install pexpect in bash in your working environment.