Force selenium to use the portable firefox application - python

I have installed firefox 14 and has firefox portable version 25.0.1 on the machine, where I run tests for a web site.
Due to a limitation in the site I'm testing, I cannot run my tests on firefox 14 installation. Also I cannot upgrade the firefox 14 installation.
So I'm looking into a solution where I can use this portable firefox version instead of the installed firefox 14 version.
How should I force selenium to use this portable version and not the installed version? If someone could direct me to some descriptive article/blog that would be great.
My code goes like:-
* Variables *
${SELENIUM_HUB} remote_url=http://127.0.0.1:4444/wd/hub
${BROWSER} firefox D:\\Firefox Portable\\FirefoxPortable\\firefox.exe
${CLIENT_URL} https://abcd.aline.local
Open Browser ${CLIENT_URL} ${BROWSER} ${SELENIUM_HUB}
Specifying path as, D:/Firefox Portable/FirefoxPortable/firefox.exe does not work because '/' get's removed. Any thoughts?
PS: python is the used language

You can specify the path to the firefox binary you want with the FirefoxBinary class passed as the firefox_binary parameter when instantiating your Firefox webdriver.
http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_firefox/selenium.webdriver.firefox.firefox_binary.html
and
http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_firefox/selenium.webdriver.firefox.webdriver.html#module-selenium.webdriver.firefox.webdriver
Make sure the path to the binary is correct with something like:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
firefox_binary = FirefoxBinary("D:\\Firefox Portable\\FirefoxPortable\\firefox.exe")
driver = webdriver.Firefox(firefox_binary=firefox_binary)
Using robotframework something like:
${firefox_binary}= Evaluate sys.modules['selenium.webdriver.firefox.firefox_binary'].FirefoxBinary("D:\\Firefox Portable\\FirefoxPortable\\firefox.exe") sys, selenium.webdriver.firefox_binary
Create Webdriver Firefox firefox_binary=${firefox_binary}
may work.

Selenium2Library does not let you specify browser path in the Open Browser keyword, but it does have remote_url argument that can be useful. Before Selenium2Library got proper PhantomJS support the way to use PhantomJS was through that remote_url, like this http://spage.fi/phantomjs
So in theory we should be able to use portable Firefox first by launching our Firefox and then connecting to that using the remote_url. Something like this.
Start Process c:\\path\\to\\portable\\firefox.exe
Open Browser http://google.com firefox main browser http://localhost:${firefox webdriver port}
The problem is that I do not know what webdriver port Firefox uses by default or how to specify it. Also installing the webdriver.xpi addon for Firefox might be necessary. The add-on can be found from here C:\Python27\Lib\site-packages\selenium\webdriver\firefox or what ever is the place where your python installation is.
There is a Create Webdriver keyword in Selenium2Library which does allow us to specify firefox_binary (among other arguments). So in theory
Create Webdriver Firefox firefox_binary=c:\\path\\to\\portable\\firefox.exe
Should work, but all I get from that is "AttributeError: 'str' object has no attribute 'launch_browser'".
Sorry that I could not figure out way to do this, but by digging a bit deeper about Firefox webdriver port or how Create Webdriver actually works you might get further.

Related

How to solve Selenium WebDriver Error using Python?

I am learning Python to tried to do an extraction of data from another website. However, I wrote a simple code to try to open a Chrome browser window and display google on my web browser.
I have seen in other videos that it is only needed to write the following code to get this to work:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.google.com")
However, when I try to run it on my PyCharm, I got plenty of error lines. The same happen if I try to run it through my command prompt (both stating the same error - pictures below). I do not know what I am missing, but I should mention that I have downloaded the packages pip, selenium, selenium-chromedriver and I have also downloaded ChromeDriver separetely from the website https://chromedriver.chromium.org/
Could anyone please guide me through this issue? Thanks a lot everyone for your precious help and your time!
Kind regards,
Salvador
ChromeDriver_executable_location
Command_prompt
Python_codeError1
Python_codeError2
Packages_installed
Please post the errors so we can see what it is tell you is wrong.
I am also new to selenium and learning python.
First, make sure your Chromedriver is located in the same folder as your script.
Second, make sure your version of Chrome and Chromedriver are compatible.
Also, when the script is finish it will close the browser immediately.
I ran your posted code and it works for me. Did you have any errors when installing the packages?
I'm not sure why you get an error, but I know a workaround that might work for you.
Instead of using the webdriver file, you can use a service to install it for you, like this:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
You need to install the webdriver_manager library first using pip
Hope this helps
You could place the Chromedriver in system path and try to execute.
Go to Advanced System Settings.
Click on Environment variables.
Select Path variable and click on Edit.
Add the Chromedriver folder path.
Suppose the path for chromedriver.exe file is - C:/project/Chromedriver/chromedriver.exe, place C:/project/Chromedriver in the Path variable.
Restart the computer and then try to execute below lines:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.google.com")

Selenium webdriver is opening the browser, but not opening the given url

This is the screenshot of the error I'm getting.
I have recently taken up the task to learn functional testing and web automation using selnium webdriver with python. When I execute my code, the web browser opens up, but the URL doesn't. I have tried all the suggestions on the internet such as: updating chrome, trying a different IDE, using FireFox. None of these have helped my code carry out what I want.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chrome = webdriver.Chrome(executable_path='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome')
chrome.get('https://www.youtube.com/watch?v=oM-yAjUGO-E')
For anyone encountering this issue, please make sure that you have installed the chromedriver (or whatever driver is available for your browser). For chrome you first need to check your information and find out the version from chrome://version/
then from, https://sites.google.com/a/chromium.org/chromedriver/downloads
download the corresponding driver and copy the filepath into the the executable_path section.
Ensure you have installed chromedriver.
The install path would typically be "/usr/local/bin", although whatever the path, that's the one to use as "executable_path"
Also remember to keep Chrome browser and chromedriver versions in sync i.e. if you have Chrome version 81, you should install chromedriver version 81.
I also have encountered this trouble. Before that, you're supposed to check the status code of your access. If it is 400, this station may set anti reptile which preventing any operation of reptile. I solved that by adding following code:
chrome.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
"""
})

How to setup chromedriver inside RobotFramework with chromedriver_binary

Looking at https://pypi.org/project/chromedriver-binary/,
to set chromedriver_binary in python is:
from selenium import webdriver
import chromedriver_binary # Adds chromedriver binary to path
driver = webdriver.Chrome()
driver.get("http://www.python.org")
assert "Python" in driver.title
How to do the same in robot framework?
After installing the dependency by pip, add this command Before call Open Browser:
Evaluate chromedriver_binary.add_chromedriver_to_path() modules=chromedriver_binary
Then you can run Open Browser
It is possible to provide the executable at the instantiation of the chromedriver. In Robot Framework SeleniumLibrary this is done using executable_path parameter. This then results in the following example:
Create Webdriver Chrome executable_path=C:/WebDrivers/chromedriver.exe
Go To http://cnn.com
Additionally when the download of the library fails, it would register as a test failed. In my view the test should not have started if the minimum requirements for running the test are not met. The download of the chromedriver is in my view one of those requirements.
In it's current form the version that chromedriver_binary is hard coded to 80.0.3987.16.0. It is recommended that you are able specifically target a version yourself when starting your scripts. For this reason I'd recommend making a configurable download for chrome driver, if you must.

Change log level of Geckodriver for Python Selenium

It appears Selenium 3.0 and above requires geckodriver with Firefox under Python 2.7., which I now have installed. However, it appears that running Selenium with Firefox now automatically creates the file geckodriver.log in the directory running the Python script.
I'd like to stop this from happening. I've looked around at various Github threads looking for an answer, but can't find anything for Firefox for Python. What I could find in geckodriver --help is to set the log level to any of the following:
--log <LEVEL>
Set Gecko log level [values: fatal, error, warn, info, config, debug,
trace]
However, I'm not sure how to do this. Perhaps using something like desired_capabilities or service_args for webdriver.Firefox()?
This did the trick for me icw geckodriver 0.19.1
from selenium.webdriver.firefox.options import Options
opties = Options()
opties.log.level = 'trace'
browser = webdriver.Firefox(options=opties)
I've just hit trouble trying to increase this log level, but the easiest way to stop it log anything is to redirect to /tmp (or even /dev/null):
webdriver.Firefox(log_path='/tmp/geckodriver.log')

How do I install ChromeDriver on Windows 10 and run Selenium tests with Chrome?

We have an Ubuntu server which we use for running Selenium tests with Chrome and Firefox (I installed ChromeDriver) and I also want to run the tests locally on my Windows 10 computer. I want to keep the Python code the same for both computers. But I didn't find out how to install the ChromeDriver on Windows 10? I didn't find it on the documentation [1, 2].
Here is the code that runs the test in Chrome:
import unittest
from selenium import webdriver
class BaseSeleniumTestCase(unittest.TestCase):
...
...
...
...
def start_selenium_webdriver(self, chrome_options=None):
...
self.driver = webdriver.Chrome(chrome_options=chrome_options)
...
I also found How to run Selenium WebDriver test cases in Chrome? but it seems to be not in Python (no programming language is tagged, what is it?)
Update #1: I found some Python code in https://sites.google.com/a/chromium.org/chromedriver/getting-started, but where do I put the file in Windows 10 if I want to keep the same Python code for both computers?
Update #2: I downloaded and put chromedriver.exe in C:\Windows and it works, but I didn't see it documented anywhere.
As Uri stated in the question, under Update #2, downloading the latest release of chromedriver and placing it in C:\Windows corrects the issue.
I had the same issue with Chrome hanging when the browser window opens (alongside a command prompt window).
The latest drivers can be found at:
https://sites.google.com/chromium.org/driver
The version in the chromedriver_win32.zip file is working on my 64-bit system.
Download the chromedriver.exe and save it to a desired location
Specify the executable_path to its saved path
The sample code is below:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(executable_path="path/to/chromedriver.exe", chrome_options=options)
driver.get("example.html")
# do something here...
driver.close()
As Uri stated in Update #2 of the question, if we put the chromedriver.exe under C:/Windows, then there is no need to specify executable_path since Python will search under C:/Windows.
Let me brief out the requirements first.
You need to download the chrome web driver zip from here. https://chromedriver.storage.googleapis.com/index.html?path=2.33/
Extract the file and store it in a desired location.
Create a new project in Eclipse and include the following code in your class.
System.setProperty("webdriver.chrome.driver", "C:\\temp\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Explanation : System.setProperty(key,value):
Key is default and same for all the systems, value is the location of your chromedriver extract file.

Categories