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.
Related
I wrote a small automation script using Python and Selenium Web Driver and I made it into an executable and the executable worked fine on my computer but when I tried it on another computer I got that chromedriver isn't in path (note: I included chromedriver using the add binary command and both computers are running the same operating system: Mac OS)
I am not quite sure why this happened, my suspicion is the following:
In my .py code I used the following two lines to initiate the chromedriver
path = "/Users/sergio/chromedriver"
driver = webdriver.Chrome(path)
However, the other computer doesn't even have this path so might be causing it even has this path. Now one might suggest simply using driver = webdriver.Chrome() but here comes the bigger problem: I tried having my chromedriver in a system path like /usr/local/bin and when I did that driver = webdriver.Chrome() worked but compiling the program using pyinstaller didn't work, I ran:
pyinstaller --onefile --noconsole --add-binary 'usr/local/bin/chromedriver:.' finalregtool.py
but I got a lot of errors including Unable to find "/Users/sergio/usr/local/bin/chromedriver" when adding binary and data files
This led to me thinking that pyinstaller automatically adds /Users/sergio to the given path which makes my path wrong so I had to use something inside /Users/sergio , I tried putting my chromedriver in Applications but compilation resulted in not finding the path again, for some reasons compilation worked when the path was /Users/sergio/chromedriver so I went with this path but the code didn't work anymore (chromedriver not found in path) so I had to specify the path manually.
Now I am kind of stuck in a deadlock not knowing what to do. Any help would be appreciated. I hope my explanation was clear enough.
My only end goal is compiling my selenium script and running it on other computers without having them install anything extra, I don't care how this should be done so if anyone has a solution outside pyinstaller I don't mind.
Update: added a bounty
There're couple of good solutions for that issue:
1.Install webdriver-manager package:
pip install webdriver-manager
what I prefer mostly and use it like:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
2.Launch Remote Selenium Hub called Selenoid and lauch browser remotely:
driver = webdriver.Remote(command_executor='ip_of_your_selenoid_instance:4444/wd/hub',desired_capabilities=DesiredCapabilities.CHROME)
3.You can launch Chrome docker container and also going to by remote url, what will be always localhost:
driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',desired_capabilities=DesiredCapabilities.CHROME)
You need to launch a standalone chrome browser:
docker run -d -p 4444:4444 selenium/standalone-chrome
and then in your python script launch browser using Remote webdriver:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote("http://127.0.0.1:4444/wd/hub", DesiredCapabilities.CHROME)
docker-compose:
# docker-compose.yml
selenium:
image: selenium/standalone-firefox
ports:
- 4444:4444
pyinstaller unpacks everything into a temporary directory during execution of exe, when using the --onefile mode. Refer to this answer and the related post to get an idea about how to get the correct relative path. You might have to use
path = resource_path("chromedriver")
Also, you can directly add the chromedriver to PATH environment variable in the runtime so you wouldn't have to specify the path explicitly and you can simply call webdriver.Chrome()
os.environ["PATH"] += os.pathsep + resource_path("chromedriver")
pyinstaller conciders usr/local/bin/chromedriver:. as a relative path, thus adds /Users/sergio in an attempt to make it absolute, try using a different location. This location doesn't matter anyway, once this file is found pyinstaller will add this to the root directory of the compiled exe, and unpack it when the exe is executed.
The modern way to deal with the chromedriver binary is to use WebDriverManager.
First you need to install the manager:
pip install webdriver-manager
Then you use it (for Chrome):
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
In my .NET Core project I use not the most modern approach I have described, but the latest stable NuGet from here https://www.nuget.org/packages/Selenium.WebDriver.ChromeDriver/.
Basically the nuget contains the chromedriver and copies it every time to your assembly folder (\bin\Debug\netcoreapp3.1 in my case). This is the line I have in my project:
"<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="*" />"
The star means the latest (highest number) stable package will be used as explained here. Of course you can install the latest version manually, and then on each 2 major Chrome versions to update the NuGet package reference so that is matches your Chrome version. (87 chromedriver works with 87 and 88 Chrome, but not with 89).
Then the driver is instantiated with this lines:
var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
References:
Error message: "'chromedriver' executable needs to be available in the path"
https://www.automatetheplanet.com/webdriver-net50/
path = "/Users/sergio/chromedriver"
driver = webdriver.Chrome(path)
Change the above code to the following format
driver = webdriver.Chrome(executable_path='C:/Users/sergio/chromedriver.exe')
from selenium import webdriver
driver = webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe")
driver.get("https://www.facebook.com/")
After a minute of running that code i get the DPC_Watchdog_Violation error, I made sure I had the right chrome version for chrome-driver, I also tried a different chrome version that goes with chrome driver but still same error.
Try running the selenium with Firefox + geckodriver combination. It worked for me and didn't crash a single time. Seems that chromedriver has a bug in it or something.
Here are some useful links to getting the correct driver version:
Correct driver version table
Driver downloads
Selenium also has an IDE which has very useful functions like action recordings. I highly recommend it as you can export python files from it and then use the generated commands without having to find all of the ids/classes.
I installed the selenium module with pip installer. Then I tried to make the code to open firefox, then open a new tab to go to google.
Code:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.google.com")
The expected result should have opened firefox and then go to google.com.
But what actually happened was the program didn't produce any error, it just didn't open firefox, and the screen wasn't frozen either.
Download a matching version of Geckodriver and unpack the geckodriver.exe to the location where current user can execute programs from (normally it's any place inside your home folder)
Amend your code to include the location of the aforementioned geckodriver like:
driver = webdriver.Firefox(executable_path="/path/to/the/geckodriver/binary")
If this doesn't help - provide the path to the Firefox executable as well:
driver = webdriver.Firefox(executable_path="/path/to/the/geckodriver/binary", firefox_binary="/path/to/firefox/binary")
Instead of steps 2 and 3 you can add both firefox and geckodriver to your OS PATH
References:
Selenium with Python - Getting Started
Selenium With Python
Selenium using Python - Geckodriver executable needs to be in PATH
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.
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.