pyvirtualdisplay, cannot open host display - python-3.x

I am trying to run python script
from selenium import webdriver
from pyvirtualdisplay import Display
Display(visible=1, size=(800, 600)).start()
driver = webdriver.Chrome(options=option)
driver.get("https://google.com")
but I am getting error "Xephyr cannot open host display. Is DISPLAY set?
It's minimal ubuntu 18.04 installation with x11 installed
also chromium-browser works only when I type startx chromium-browser, otherwise it's drop Trace/breakpoint trap (core dumped)
#update
when I run sudo xinit and run my script manually it works but in window, not fullscreen, are there another way to run it?
#update
I was trying to set options in python script to run fullscreen like "--start-fullscreen" and "--start-maximized" but it cover half of screen like here: https://unix.stackexchange.com/questions/273989/how-can-i-make-chromium-start-full-screen-under-x
also I was trying to edit Preferences for chromium like in this thread in /home//.config/chromium/Default/Preferences, but its not working, I saw that every run of my python script chromium generate new Preferences in /tmp/.org.chromium.Chromium./Default/Preferences this browser setting are different than these in /home//.config/(...)/Preferences
ofc I have delete Display option from python script
Any help?

Related

Chromedriver closes after running nohup on google cloud

I have got a simple script which uses selenium and chromedriver. I have installed the chrome. When I run the script using the command nohup python3.7 -u main.py & tail -f nohup.out everything works; script works as it should. When I close the window of the google cloud ssh the scripts stop working. When I reopen the ssh and call tail -f nohup.out I receive such an error
selenium.common.exceptions.WebDriverException: Message: chrome not reachable
(Session info: headless chrome=75.0.3770.142)
I was using the chrome version 87 and read that downgrading it should help, so I downgrade it to 75.
It is run on ubuntu, chrome and chromedriver version are 75. Is there such a command that would make chromedriver not close after closing the ssh window?
You can start processes in background in Linux. They will continue running even when you log off (which closing SSH window essentially is).
There are several ways:
Using bg & disown - as described here
Using screen as described here
You can go through entire thread to get more ideas. I'd recommend using bg & disown - it works well on Ubuntu provided by GCP however you can try out various solutions and pick the one that suits your needs the best.
From my experience - I was using screen for many things - including virtualbox and it worked - it may be more cumbersome if you want many processes to run in background but if that's just one its pretty easy.
Install screen: sudo apt install screen, run it with screen and treat it as another screen, run whatever you want and then just press ctrl + a d and you will be back to "original" shell. If you want to resume yor screen sessions type screen -r. You will find even more about using screen here.

how to run several python programs from ubuntu console?

I would like to run several python programs from a ubuntu console ..
I am using PuTTY ... to open the terminal from the server ..
I currently use npm to run the javaScripts programs. I would like to know how I can run python programs in ubuntu, and close the assignment while the program is still running.
I wish I could leave the python program running, even if I close the terminal
You need a terminal multiplexer like screen.
With:
screen -S mysession
you can start a session and launch your programs.
You can detach that session by pressing Ctrl+A followed by D.
You can get this session again with:
screen -r mysession

How to resolve XStartTimeoutError: Failed to start X on display ":1013" error

I want to run a pyvirtualdisplay on my Mac.
After installing all dependencies I installed X11 because
i needed Xvfb to run and changed some permission for some folder called ~/.Xauthority to 777.
Still, if I ran
display = pyvirtualdisplay.Display(visible=0, size=(320, 240)).start()
XStartTimeoutError: Failed to start X on display ":1013" (xdpyinfo check failed).
What I also tryed was to change
X11Forwarding in my /etc/ssh/sshd_config file to yes.
Did not work out.
How can I resolve the issue?
Constructive help appreciated.
I got this same error while trying to run Selenium using PyVirtualDisplay on my Raspberry Pi. I was not running Xephyr.
In a separate terminal window I ran
export DISPLAY=:0 XAUTHORITY=/etc/X11/host-Xauthority
and then
Xephyr :1 -fullscreen
Then re ran my python script and it worked.
If that doesn't work for you, here is where the actual error is occurring in the PyVirtualDisplay package: https://github.com/ponty/PyVirtualDisplay/blob/d229fcf892fdda17887684b977365d1fa90255eb/pyvirtualdisplay/abstractdisplay.py#L149
As you can see, you might need to install xdpyinfo in some form on your Mac.

Python3 GUI script does not work when double clicked

My GUI script that is a PyQt5 file (.pyw extension) does work when running on my IDE with a build configuration that tells the compiler to run the script with python3:
And it also works when i tell to the regular terminal on Linux to run same script with python3 like this:
When runned with the default python (python2.7) on a regular terminal it tells: ImportError: No module named PyQt5.QtWidgets.
My code does it have these lines on the start to tell that is a python3 script like: #!/usr/bin/python3 or #!/usr/bin/env python3 (I have python3 installed).
When double clicked on the Linux Mint File Explorer the cursor turns crosshair and nothing happends, with the terminal option, same happends and a empty terminal shows. Im talking these options
I guess Linux Mint still runs the scripts with python2.7 even when I added the bash lines to tell
Someone knows why the lines:
#!/usr/bin/python3
#!/usr/bin/env python3
doesnt work when just double click?
I want to run the script from the Linux File Explorer without the need of an IDE or using the terminal.
Try chmod +x file.py and run it in terminal by using ./file.py also try lunching the file from a different path, like python3 ~/path/to/file.py and see if the error persists

xvfb install on Linux

I am trying to get Selenium to do some automated browsing on a Linux application server. I am stuck at the point where my 'perl recording' from Selenium sends a request to start the browser, and the standalone selenium server throws an error - 'Timed out waiting for profile to be created!'.
I understand from browsing this error, that I could resolve this by creating a profile for the firefox browser I installed on the linux server. However, when I try to create a profile, I now get the error -
Error: no display specified
When I look this up, I find SO posts that suggest I could resolve this by installing xvfb first on the server and then setting the DISPLAY variable before starting the selenium server. However, I am not a root user, and wanted to check if xvfb can be installed locally on a user's home directory.
First need to install X window system frame buffer x server:
yum install xorg-x11-server-Xvfb.x86_64
make sure you have firefox installed, then start the x server on some display port like 99
Xvfb :99 -ac -screen 0 1280x1024x24 &
Set the DISPLAY environment to 99 by export DISPLAY=:99 in your code or maybe in bash profile.
try Xdummy to launch X server in virtual frame buffer, it does not need root.
http://www.karlrunge.com/x11vnc/Xdummy

Resources