Running Selenium without GUI : Status code 64 - python

I'm trying to run Selenium in Headless mode in a Linux machine without GUI. The problem is that I'm getting a WebDriverException and I can't find anywhere what the status code 64 means.
Does anyone know where to find the status code definitions ?
Code :
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(1024, 768))
display.start()
path = '/home/workspace/geckodriver'
driver = webdriver.Firefox(executable_path=path, service_args=['--verbose', '--log-path=/tmp/firefox.log'])
# website testing functionality:
driver.get('https://python.org')
print(driver.title)
Error :
WebDriverException: Message: Service /home/workspace/geckodriver unexpectedly exited. Status code was: 64

I'm not sure what the status code means, but try to update the Firefox webdriver. Updating the Firefox webdriver fixed it for me.

Related

Full screen Firefox Python Selenium

I'm trying to start a full screen page in Firefox with Selenium in Python 3. The page opening works fine, but when I send the F11 key to the browser (the Full Screen key), anything happens. Here is my code :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
firefox = webdriver.Firefox()
firefox.get('http://localhost')
firefox.maximize_window()
body = firefox.find_element_by_tag_name('html')
body.send_keys(Keys.F11)
Does anyone know how to make my page start in full screen ? I know it's possible with Chrome, but it's harder with Firefox
This is what worked for me.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get('http://localhost')
driver.find_element_by_xpath('/html/body').send_keys(Keys.F11)
Hope this helps
Just realized I am using Python 2.6 vs your 3. Sorry about that, but at least you know it will work on an older Python version
Selenium has a built-in method to make the window fullscreen: fullscreen_window()
Selenium API - fullscreen_window()
Invokes the window manager-specific ‘full screen’ operation
browser.get("https://www.screenku.com")
browser.fullscreen_window()
pip3 install selenium pyautogui
#!/usr/bin/env python3
import pyautogui
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("dom.webnotifications.enabled", False)
profile.set_preference("general.useragent.override", "Mozilla/5.0")
profile.update_preferences()
browser = webdriver.Firefox(firefox_profile=profile,executable_path = '/usr/local/bin/geckodriver')
browser.get("https://www.screenku.com")
pyautogui.press('f11')

Running Selenium tests on a xvfb Not being able to connect

I'm trying to run a script which run several tests using Selenium Firefox webdriver.
It works flawless in a local machine, but fail miserably running on a xvfb.
The machine is a CentOS release 6.8 (Final)
Firefox version 45.6.0
I'm using Python/Marionette
The command is similar to this:
xvfb-run --server-args="-screen 0, 1920x1080x24" MyProgram
Running this way I get several errors related to not loading the page.
So I got a few screenshots, and all I see is the "Unable to connect" Firefox screen.
At first I though it could be proxy related... I was already implicit not disabling the proxy and a simple "wget" would work as expect.
But then I forced the Firefox preference in the code so it doesn't use the proxy, for sure, right?
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 0)
Same result.
So I googled for similar situations and found some answers asking to add the display number in the command line.
So I changed the command line to it:
export DISPLAY=:1
xvfb-run --server-args=":1 -screen 0, 1920x1080x24" MyProgram
Then I got a different error, but still not working:
ERROR: WebDriverException: connection refused Traceback (most recent call last):
I have also tried to log more information adding the -e parameter to xvfb-run, but all I get is an empty file.
Any idea what else can I try to make it work?
* UPDATE *
Here's a small code to reproduce the issue
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.proxy import *
display = Display(visible=0, size=(1920, 1080))
display.start()
profile = webdriver.FirefoxProfile()
profile.set_preference("network.http.phishy-userpass-length", 255);
profile.set_preference("network.proxy.type", 0)
capabilities = None
# Marionette not necessary as it's Firefox 45
# capabilities = DesiredCapabilities.FIREFOX
# capabilities["marionette"] = True
print("Getting webdriver...")
browser = webdriver.Firefox(firefox_profile=profile, capabilities=capabilities)
print("Requesting URL...")
browser.get('https://www.google.com')
print("TITLE:", browser.title)
browser.quit()
display.stop()
The output:
Getting webdriver...
Requesting URL...
TITLE: Problem loading page

Element not visible with pyvirtualdisplay and selenium in python

I'm trying a simple browser operation where I locate a username element on a website and then try to login. I'm using selenium and python to do this. Here's some simple code that works on my own local machine. The code opens a browser on my computer and then navigates to the correct username box and enters the username.
from selenium import webdriver
browser = webdriver.Firefox()
browser.get(my_url)
username_element = browser.find_elements_by_name("USERNAME")[0]
username_element.clear()
username_element.send_keys(my_username)
However, when I try to deploy the same code on an AWS server using pyvirtualdisplay so that Firefox doesn't need to pop up, it no longer works.
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Firefox()
browser.get(my_url)
username_element = browser.find_elements_by_name("USERNAME")[0]
username_element.clear()
username_element.send_keys(my_username)
The element is definitely found, but I get the element not visible error:
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
which is confirmed by:
>> username_element.is_displayed()
False
I've tried various things I found on SO including:
making sure xvfb and xephyr are installed
adding a browser.implicitly_wait(30)
trying a WebDriverWait(browser,30).until(EC.visibility_of_element_located((By.NAME, "USERNAME"))) which times out
Any ideas on how to solve this?
You can scroll screen:
browser.execute_script("window.scrollTo(0, 600)")
Figured it out after taking a screenshot. Turns out my screen display wasn't set large enough. Changing the display size to (1600,900) solved the problem.

Selenium Stucks on pyvirtualdisplay

I am running Selenium python on server where I need to hide chrome display. Python script runs most of time but sometimes it stucks when it creates new chromedriver session. Don't have any idea why it stucks sometimes.
Snippet Code:
from selenium import webdriver
from pyvirtualdisplay import Display
chromedriver = '/usr/local/bin/chromedriver'
os.environ['webdriver.chrome.driver'] = chromedriver
display = Display(visible=0, size=(800,600))
display.start()
driver = webdriver.Chrome("/usr/local/bin/chromedriver") => Stuck here
driver.get("example.com")
I just currently set up Selenium on my server. If you get your permission right, try to put this line.
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
driver = webdriver.Chrome(desired_capabilities=options.to_capabilities())
to turn off the sandbox.

Take screenshot with selenium in Fullscreen mode

I am taking a screenshot by using selenium with no display. It's working but it would be nice if I could take a screenshot of the Fullscreen Browser (without Firefox Toolbar and so on, just the website). I tried the above code which should perform a F11 press. The code runs with no error, however Fullscreen is not working, so I guess the F11 command is somehow not executed. My OS is ubuntu.
Can somebody tell me how to take the screenshot in selenium in Fullscreen mode?
#!/usr/bin/env python
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
display = Display(visible=0, size=(1920, 1080))
display.start()
browser = webdriver.Firefox()
browser.get('http://www.google.com')
ActionChains(browser).send_keys(Keys.F11).perform()
browser.save_screenshot('screenshot.png')
browser.quit()
display.stop()
just select one element on the page and send the keys ,
elem = driver.find_element_by_name("your_element")
elem.send_keys(Keys.F11)
make sure that element is loaded in DOM.It worked for me.

Categories