Running headless firefox with selenium in Linux - python

I am trying to run a headless Firefox browser on Linux. I have firefox installed and on my PATH, xvfb is installed, and am using pyvirtualdisplay to setup the display with xvfb. When the last line is executed
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=False, size=(1024, 768))
display.start()
browser = webdriver.Firefox()
I get the 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.
I tried setting a log file as:
p = webdriver.FirefoxProfile()
p.set_preference("webdriver.firefox.logfile", "/tmp/firefox_log")
browser = webdriver.Firefox(p)
But there is no log file created (and creating the file first does not write to it). How do you find out more information for what is going wrong? How do I fix this?

from pyvirtualdisplay import Display
from selenium import webdriver
class Firefox:
def __init__(self):
self.display = Display(visible=0, size=(800, 600))
self.display.start()
self.driver = webdriver.Firefox()
self.driver.set_window_size(1120, 450)
def shutdown(self):
self.display.stop()
self.driver.quit()

I think python is not pointed towards the correct binary. I had some issues in the past like that after I deleted the binary that came with my distro and then installing the 64bit version. Yes, there are issues with 64bit version in Linux, based on my experience. Usually opens and hangs there doing nothing.
If that's your problem, get a 32bit prior to 45ff version if your testings are not based gecko driver. For ff45+ get also the gecko driver and add the binary to path(of gecko driver). Then you should use Firefox Binary this way.
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary("/usr/bin/firefox") #Or whatever path you have(E.G. Portable)
driver = webdriver.Firefox(firefox_binary=binary)
On windows I use portable apps. On linux you have to make the binary portable, there is a thread, here on Stack, about that. Altough is not necesary
Cheers

Related

Selenium Opens Browser But Refuses to Execute the Next Line of Code

It looks like after opening the browser, Selenium cannot move on for some reasons I can't figure out. No error was ever displayed.
Here is my simple code:
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# Using Chrome to access web
browser = webdriver.Chrome(executable_path = "C:\Program Files\Google\Chrome\Application\chrome.exe")
print ("done")
# Open YouTube website
browser.get("https://www.youtube.com/")
The browser opens just fine, but the print("done") statement is never executed. (In the terminal the word "done" was never printed. So, it infers that the selenium has never finished executing the command to open the browser even though the browser has opened, and I have waited for several minutes.
Thanks in advance to our wonderful StackOverflow community!
The reason is because you are using chrome.exe which is for browser. Instead you should download chromdriver.exe, Please download from here. You should download Latest stable release: ChromeDriver 94.0.4606.61 (as on 3rd-oct-2021). Keep that in your automation directory and any directory of your preference.
driver_path = r'C:\\Users\\username\\Desktop\\Automation\\chromedriver.exe'
driver = webdriver.Chrome(executable_path = driver_path)
Note that, in place of driver_path, you should give the path where you've kept the chromdriver.exe
You should use chromedriver.exe instead of the path to your chrome.exe.
Download the chromedriver suitable for your chrome version, from here:
donwload Chromedriver.exe
Afterwards, do something like this:
browser = webdriver.Chrome("E:\YourPathToChromeDriver\chromedriver.exe")
Try these steps:
Check your Google Chrome version here "chrome://settings/help"
Download chromedriver.exe from "https://chromedriver.chromium.org/downloads"
Change executable path to newly downloaded file.
As mentioned above, this issue is the chrome.exe. You need to use a chromedriver instead. You can download one manually here https://chromedriver.chromium.org/downloads and then set the path to it like you did to the chrome.exe.
However, instead of downloading the chromedriver manually, I recommend using a library on GitHub which does that for you and loads it from cache if there is already one installed. (I'm not the owner nor the maintainer of this repository, but I do find it rather simple to use.)
https://github.com/SergeyPirogov/webdriver_manager
First you'd need to pip install webdriver-manager and then you can use it as following:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())

How can I choose which firefox executable to use among multiple firefox binaries through Python selenium

I'm using python (v 3.65) selenium (v3.11.0) on Mac OS X (v 10.11.6) with firefox (v 59.0.3) and geckodriver (v 0.20.1). I have my usual firefox in the Applications folder and a second firefox in another folder. How can I tell python selenium to use the second firefox instead of going to the one in Applications?
I'd prefer an answer that generalises to other browsers besides firefox/geckodriver, if possible.
To choose and use one of the Firefox executable among multiple you can use the argument binary_location from firefox.options. As an example in the following code block I have used the Firefox Nightly binary to open the Firefox Nightly browser :
Code Block :
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.binary_location = r'C:\Program Files\Firefox Nightly\firefox.exe'
driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get('http://google.com/')
print("Firefox Browser Invoked")
driver.quit()
Console Output :
Firefox Browser Invoked

Geckodriver not being found

I am relatively new with this, and I am currently using python 2.7 with selenium. When running the following:
from selenium import webdriver
browser = webdriver.Firefox()
I get an error saying that 'geckodriver' executable must be in PATH. I then put it in /usr/local/bin, and it still did not work. I also tried doing browser = webdriver.Firefox(executable_path=r'your\path\geckodriver'), and it still did not work. Additionally, after using chmod +x geckodriver in the terminal, it would give me an error. What solution will fix this problem?
From your question it is not clear about the OS on which you are triggering your scripts. While you work with Selenium 3.5.x you can download the geckodriver binary and place it anywhere within your system and reference it by providing the absolute path of the geckodriver binary while you initialize the webdriver instance. On my Windows 8 Pro system this is the working code:
from selenium import webdriver
driver=webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("http://www.google.com")
print("Page Title is : %s" %driver.title)
driver.quit()
Console Output:
Page Title is : Google
Perhaps on Linux systems, we have to do:
from selenium import webdriver
driver=webdriver.Firefox(executable_path=r'/absolute_path/geckodriver')
driver.get("http://www.google.com")
print("Page Title is : %s" %driver.title)
driver.quit()

Python test using selenium cannot perform simple test

I am trying to learn about Selenium but I am not able to get even a simple program to test. Selenium webdriver seems to not be cooperating with Firefox and I am very frustrated, so I come to Stack Overflow for help.
For background, I use Python, can install with pip, and know command line.
I am on windows 10, firefox 48, and selenium webdriver 3 with python 3.5.2
Whenever I run the selenium test, (it opens a Firefox windows and the selenium website)
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.seleniumhq.org')
I always get an error:
selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Profile Dir: C:\ ... \AppData\Local\Temp\tmp68m5rtwt If you specified a log_file in the FirefoxBinary constructor, check it for details
It also opens a firefox window and has the link of about:blank&utm_content=firstrun(not a valid url)
I have looked across the internet for a similar situation, but nothing really close. I also tried many tutorials and made sure that I installed selenium the right way. I noticed that firefox was recently updated, but I am not sure if this has any effect.
I would appreciate any help for this, and instructions for what I should do.
Firefox 48+ doesn't support webdriver.Firefox().
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX
caps["marionette"] = True
caps["binary"] = "path/to/your/firefox"
browser = webdriver.Firefox(capabilities=caps)
browser.get('http://www.seleniumhq.org')
This is what I was trying
1. download geckodriver.https://github.com/mozilla/geckodriver/releases. v.0.10.0is for selenium 3(beta).
2. add PATH your geckodriver.
4. rename it to wires
5. restart shell
6. check the version
$ wires --version
7. and run above the code.

Selenium test with Python in Internet Explorer

I have written a script that opens a web browser using python and Selenium. It works fine with Firefox using the following code:
from selenium import webdriver
driver = webdriver.Firefox()
When I replace Firefox with IE (the suggested value when I start typing), I get the message IEDriver executable needs to be available in the path.
from selenium import webdriver
driver = webdriver.IE()
Download IE Drivers based on your OS (Windows 32 or 64 bit)
a. Download Windows 32 bits driver
OR
b. Download Windows 64 bits driver
Extract the zip and copy IEDriverServer.exe file to some location e.g. E:\IEDriver
Write the following script
from selenium import webdriver
browser = webdriver.Ie("e:\\IEDriver\\IEDriverServer.exe")
Run the script, it should open IE browser...
Selenium with Python bindings in IE:
There are 2 ways to run Selenium python tests in Internet Explorer. I'm considering Windows (Windows 10 in my case):
Prerequisite: Download IE Driver based on your OS from the site: http://docs.seleniumhq.org/download/
32 bit Windows IE
64 bit Windows IE
Way 1:
i) Extract the downloaded zip file in a directory/location of your choice
ii) Set the executable path in your code as below:
self.driver = webdriver.Ie(executable_path='D:\Selenium_RiponAlWasim\Drivers\IEDriverServer_x64_2.42.0\IEDriverServer.exe')
OR,
self.driver = webdriver.Ie("D:\\Selenium_RiponAlWasim\\Drivers\IEDriverServer_x64_2.42.0\\IEDriverServer.exe")
Way 2:
i) Simply paste the IEDriverServer.exe under /Python/Scripts/ (In my case the folder was: C:\Python36\Scripts)
ii) Now write the simple code as below:
self.driver = webdriver.Ie()
It means exactly that. Selenium needs the executable to work with IE.
A quick google search led me to this. You need to download the executable and place it somewhere visible. Also, taking a look at this should help clear some things about PATH variables.
In selenium 4 you would need to use service method otherwise you would get depreciated error: You no longer need to download IE driver.
Here are compatible codes for Selenium 4.x IE:
# Internet Explorer Browser version
from selenium import webdriver
from selenium.webdriver.ie.service import Service
from webdriver_manager.microsoft import IEDriverManager
driver = webdriver.Ie(service=Service(executable_path=IEDriverManager().install()))
driver.get('https://www.google.com')

Categories

Resources