Xvfb on Windows - python

I'm using pyvirtualdisplay to run a test with a headless Firefox browser. This is the code I'm using :
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from pyvirtualdisplay import Display
display= Display(visible=0, size=(320, 240)).start() # visible=0
display.start()
driver = webdriver.Firefox()
driver.get("https://google.com")
display.quit()
And the traceback that I obtain :
easyprocess.EasyProcessCheckInstalledError: cmd=['Xvfb','-help']

You can't use pyvirtualdisplay on Windows.
It is just a wrapper that calls Xvfb. Xvfb is a headless display server for the X Window System. Windows does not use the X Window System.

For windows users you can use the free VNC utility.. For example if you are running docker you can do it in 3 steps:
Run a docker image that has the standalone firefox server (that has port 5900 exposed for VNC)
$ docker run -d -p 4444:4444 -p 5990:5990 selenium/standalone-firefox-debug
Open VNC and connect to that host localhost:5990, password is 'secret'
Now simply execute your selenium script and you will see what's happening in VNC window live as its happening. Just make sure the script is pointing to your docker standalone server like localhost:4444/wd/hub in order for it to work

Related

Run selenium with chrome driver on centos without Gui

I write some code with python selenium using chrome driver as web driver. When I run code in my system ( mac os ), the code works. Chrome browser was opened, and selenium works correctly. But when I want to run my code in Centos 7 without GUI, selenium can't match with web driver and run the code. Although I install google-chrome-stable and use the same web driver for it, selenium can't run web driver.
driver = webdriver.Chrome(driver_path=chromedrive75)
So, I don't know how to fix the code and running on my centos operating system.
#Vahid, please check the chrome web browser version which is compatible with server, also try running in headless mode.

How to maximize window in Jenkins using Python, Selenium Webdriver and Chromedriver?

How to maximize a window in Jenkins?
I tried to maximize window using:
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument("--start-maximized")
self.driver = webdriver.Chrome(chrome_options=chromeOptions)
driver.maximize_window()
driver.set_window_size(1920,1080)
driver.fullscreen_window()
All works when I run the test in PyCharm, but in Jenkins, the window does not change size.
The problem is with Jenkins running as a windows service.
In Windows command prompt, go to folder where jenkins-cli.jar file is located.
And stop the service.
java -jar jenkins-cli.jar -s http://localhost:8080 safe-shutdown --username "YourUsername"
--password "YourPassword"
Have Jenkins run from the command prompt using a script.

Python Selenium Chrome Not Running Headless

I'm currently writing a program in which I'm connecting to chrome. I would like to make this process without a new chrome window popping up every time. I know that --headless makes chrome run silently, but my current code is not working; it is still opening up chrome tabs.
Current Code:
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options = chrome_options)
What am I doing wrong or what do I need to add?
If you are using linux one alternate solution to run the browser in headless mode is to use pyvirtual display.
First install xvfb and pyvirtual display:
sudo apt-get install xvfb xserver-xephyr
sudo pip install pyvirtualdisplay
The code will be:
from pyvirtualdisplay import Display
import webdriver
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Chrome()

Schedule selenium to run on a remote server

I need to run selenium on a remote server since the server doesn't have a screen I'm using PhantomJS as a web driver. this is the code I Have:
import time
import os,sys
reload(sys)
sys.setdefaultencoding('utf8')
import re
from selenium import webdriver
url = 'https://wiki.python.org/moin/HowTo/Sorting'
driver_1 = webdriver.PhantomJS()
driver_1.get(url)
content = driver_1.page_source
On my computer, it works fine, but after that, I'll open other pages so I need the code keep running for a while.
I'm running the script from the terminal as:
python my_script.py
And the script will execute on the same terminal from where I'm accessing the server so either I won't be able to disconnect or if a loss the connection the script will stop, how can I handle this issue?
You do not need a read GUI on the server in order to run Selenium with any browser. Selenium can easily run on any server with real browsers like Chrome or Firefox.
Here is some code that should help you make it working:
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
driver = webdriver.Chrome() # Or Firefox()
If you want your script to continue executing even if you lose connection to the server, you can easily do this with terminal software like Tmux or screen

py2app selenium without firefox install

I am wondering if there is a way within py2app to include the Firefox browser, or if there is a way for Selenium to use Firefox without having to install Firefox on the host machine.
I have created an app with py2app that uses Selenium, however, I have Firefox installed on my machine, but not everyone that will receive the app will have Firefox installed. I am looking for a way to either include Firefox in the distribution or go around this.
Script will not run if Firefox is not preinstalled.
You can test your script with other browser, for example Chrome. If it works on Chrome also, then you can edit script like this:
from selenium.common.exceptions import WebDriverException
try:
driver = webdriver.Firefox()
except WebDriverException:
driver = webdriver.Chrome()
You can add same for few more browsers (IE, Opera, Safari...) to be sure that script will run on users machine

Categories

Resources