Headless Selenium + Xvfb + Chrome on OSX 10.11 - python

Okay, so first I learned that Xvfb wasn't included with my OS X version, so I installed it from http://www.xquartz.org/.
and that seemed to have worked:
which xvfb
/opt/X11/bin/xvfb
But when I try using it with either pyvirtualdisplay and xvfbwrapper, following advice I found on this question How do I run Selenium in Xvfb? My script runs without errors but just opens in a Chrome browser window:
from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Chrome()
browser.get('google.com')
Am I doing something wrong here?

I believe that Chrome is built for Quartz ui framework, so it ignores the X11 windowing engine. You will need to install an X11 version of a browser and then execute that.

For me, this code works fine on OSX 10.13. You don't need pyvirtualdisplay, because you can run chrome in headless mode. Just download chromedriver that fits to your chrome version and put it to usr/local/bin
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
options.add_argument('--mute-audio')
options.add_argument('--lang=de')
options.add_argument('--window-size=800,600')
options.add_argument('--disable-notifications')
options.add_argument('--enable-popup-blocking')
browser = webdriver.Chrome(chrome_options=options, executable_path='/usr/local/bin/chromedriver')
browser.get('some url')

Related

unable to run selenium chrome headless mode in Windows 7 enterprise

I am trying to run selenium chrome headless mode in Windows7 enterprise edition. I have:
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')

options.add_argument('--disable-gpu')

options.add_argument('window-size = 1920,1080')
driver = webdriver.Chrome(executable_path="chromedriver.exe", options=options)
I get:
chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed
The same set of commands works perfectly on my mac book pro
anyone knows why?
I think you should try with full file path at this line :
driver = webdriver.Chrome(executable_path="/full file path/chromedriver.exe", options=options)

Selenium and Firefox issues [duplicate]

There are about 100 posts about the same issue but none of them seem to work for me, hence asking again. I'm trying to launch a Firefox browser using Python and Selenium and I get the following error:
WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.
I tried each and every answer on the web but nothing seems to work.
This is my code:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX
caps["marionette"] = False
binary = FirefoxBinary('d:\\Desktop\\IEDriver\\geckodriver.exe')
options = Options()
options.set_headless(headless=True)
driver = webdriver.Firefox(firefox_binary=binary, firefox_options=options, executable_path=r'd:\\Desktop\\IEDriver\\geckodriver.exe')
driver.get("http://google.com/")
print ("Headless Firefox Initialized")
driver.quit()
If I set caps["marionette"] = True then the error I get is
SessionNotCreatedException: Message: Unable to find a matching set of capabilities
Versions of software I'm running:
Firefox: 62.0 (64 bit)
Selenium: 3.14.0
Gecko: 0.21.0
Python: 3
OS: Windows 8.1 64 bit
Any help would be highly appreciated.
EDIT: I've uninstalled and re-installed Firefox but didn't work. Also tried installing Firefox 61.0.2, still no luck.
This error message...
WebDriverException: Message: The browser appears to have exited before we could connect.
If you specified a log_file in the FirefoxBinary constructor, check it for details.
...implies that the GeckoDriver was unable to initiate/spawn a new WebBrowser i.e. Firefox Browser session.
You need to take care of a couple of things as follows:
To set the FirefoxBinary you need to use the FirefoxOptions() and instead of passing the absolute path of geckodriver binary, you have to pass the absolute path of the desired firefox binary.
As you are using GeckoDriver v0.21.0 you have to mandatorily use marionette so either keep it unchanged (by default true) or set marionette to true.
Your own code with incorporating the minor changes will be:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
binary = r'C:\Program Files\Mozilla Firefox\firefox.exe'
options = Options()
options.set_headless(headless=True)
options.binary = binary
cap = DesiredCapabilities().FIREFOX
cap["marionette"] = True #optional
driver = webdriver.Firefox(firefox_options=options, capabilities=cap, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
driver.get("http://google.com/")
print ("Headless Firefox Initialized")
driver.quit()
Console Output:
Headless Firefox Initialized
Here you can find a detailed discussion on Unable to find a matching set of capabilities with selenium 3.4.3, firefox 54.0 and gecko driver 0.17
Make sure (especially on Windows (Win 10)) that your browser and controller (python/C/java/perl/etc) is all either x64 or win32, Microsoft will not thunk between them anymore.
So, if your trying to control a 64 bit browser (what will be downloaded by default from firefox) from a x32 bit python, it will exit before you can connect.. go and install a win32 version of firefox for the magic to happen
After trying almost all of the answers on different forums, a simple self trial resolved the problem and i.e. you need to have python, firefox browser and geckodriver in either 62 bit or 32 bit. Mismatch in this caused the problem in my case.
After ensuring that you are using the same bit version for all the 3 components, just use following lines to run firefox:
ffPath = "C:\\Drivers\\geckodriver.exe"
os.environ["webdriver.firefox.driver"] = ffPath
driver = webdriver.Firefox(executable_path=ffPath)
driver.get(url)
The issue for me was the mis-match version between python and gekodriver . once all the involved party were 64 bit it worked like a charm
This error has troubled me a lot.
Install this:
pip install -U selenium
binary = r'C:\Program Files\Mozilla Firefox\firefox.exe'
options = Options()
options.binary = binary
browser = webdriver.Firefox(firefox_options=options, executable_path=r"C:\Drivers\geckodriver.exe")

using selenium without browser screen - python windows

i use selenium for instagram appicatin.
i wonder there is any way to run selenium without browser opening in windows?
there is some solution in linux like install Xvfb but i don't see any solution for windows!
Do you mean that --headless?
You can do some operations without opening Chrome if you have chrome-driver.
Here is how to set headless:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chromeoption = Options()
chromeoption.add_argument('--headless')
browser = webdriver.Chrome(options=chromeoption)
# Then it is your work.
browser.get(url="xxx")

how do you see the Chrome webdriver using selenium

I followed setting up chrome requirements for selenium.webdriver.Chrome, and I used the following code Running webdriver chrome with Selenium:
import os
from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
driver = webdriver.Chrome()
driver.get("http://www.google.com")
print driver.page_source.encode('utf-8')
I was very surprised to see that the code works, but no browser pops up. I would think this is almost impossible to develop, as I can't see what I'm doing right/wrong. How do I use selenium to actually see a functional Chrome driver (python)? Thank you
ANSWER:
set visible to 1, install emulator:
sudo apt-get install xvfb xserver-xephyr
display = Display(visible=1, size=(800, 600))
In java we add these two lines for executing code in chrome
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
not sure if there something similar for python as well!

Why is Firefox's url address bar so small when Firefox is opened by Selenium?

I am trying to test a webpage with Selenium. My code is below.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)
driver.close()
When I run the above code, a Firefox browser window pops up as expected. However, the size of the window's fields are very small (see picture below). So small that the left most tab runs into the window's zoom button. This sizing issue does not occur when I use the Chrome WebDriver. Has anyone had this sizing issue? Any has anyone found out how to solve this issue?
Here is my mini-stack:
selenium 2.42.1 (installed via pip)
python 2.7.6
firefox 29.0.1
Mac OSX 10.9.3
This is the issue with the latest selenium package version:
Selenium launches FF with tiny font on OSX Macbook
As a workaround, downgrade selenium to 2.40.0 version:
pip install selenium==2.40.0

Categories