Selenium driver plugin using Java - selenium

I was getting the error:
The path to driver executable must be set by the webdriver.gecko.driver system property
Then i downloaded geckodriver and set the path as below. After that the code is working fine.
But my question is do i need to use the below code every time (in each and every program) when i want to launch a firefox using selenium code?
System.setProperty("webdriver.gecko.driver","<path to geckodriver.exe>");
WebDriver driver = new FirefoxDriver();

When we work with Selenium 3.x, geckodriver and Mozilla Firefox Browser through Selenium-Java bindings we need to configure the Test Environment through System.setProperty line. Find the details along with your Answer below.
Your Question have 2 parts so I will answer both of them in parts:
1. Do I need to use the below code every time in each and every Program:
System.setProperty("webdriver.gecko.driver","<path to geckodriver.exe>");
WebDriver driver = new FirefoxDriver();
Answer:
Yes
Explaination:
Whenever we need to execute a program (Selenium-Java based) it is mandatory we explicitly mention the type of driver (gecko, chrome, ie) which we are trying to use in our program in the form of "webdriver.gecko.driver". Along with it, we also need to explicitly mention the absolute path of the driver (gecko, chrome, ie) binary (.exe) in the form of "<path to geckodriver.exe>". Next we are using the WebDriver interface and casting the WebDriver instance to FirefoxDriver.
2. Do I need to use the below code every time when i want to launch Firefox:
System.setProperty("webdriver.gecko.driver","<path to geckodriver.exe>");
Answer:
No
Explaination:
Once we configure the WebDriver instance i.e. the driver through DesiredCapabilities class, the driver is able to carry the same configuration till its life time which is controled through your Automation Script. So until and unless we explicitly call quit() method through the driver, the driver instance remains active and carries the configuration. So, within your program no matter how many time you choose to close the browser instance by calling close() method, you can always mention driver = new FirefoxDriver(); to open a new browser session again and again with the stored configuration within the driver.
An Example:
package demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Driver_Close_Initiate
{
static WebDriver driver;
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability("marionette", true);
driver = new FirefoxDriver(dc);
driver.get("https://google.com");
driver.close();
driver = new FirefoxDriver(dc);
driver.get("https://facebook.com");
driver.quit();
}
}

The reason you need to use the gecko driver is your Firefox version is greater than v47. So to answer your question, if you don't want to use the gecko driver everytime, you need to downgrade your Firefox version to 47 or below.
Previous versions can be found here:
https://ftp.mozilla.org/pub/firefox/releases/

Yes you have to use setProperty(String key, String path) every time if you want to use Firefox version above 47 and selenium jars above 3.0.
System.setProperty("webdriver.gecko.driver", "Path of geckodriver.exe");
or, you can set the configurtion path using DesiredCapabilities classs
DesiredCapabilities des_capablity = DesiredCapabilities.firefox();
des_capablity.setCapability("marionette", true);
driver = new FirefoxDriver(des_capablity);

Related

Selenium Python firefox webdriver

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", "localhost");
profile.setPreference("network.proxy.http_port", 3128);
WebDriver driver = new FirefoxDriver(firefox_profile=profile);
I am using below code to config but it's giving an error. You should need to be include executive path. How can I solve it. I am Running this code in pycharm.
You can also see the image for more brief
You need to specify the path.
Try this out option 1
option 2
from selenium import webdriver
browser = webdriver.Firefox(executable_path = '/path/to/geckodriver')
browser.get('https://www.google.com')

How to install extension permanently in geckodriver

I need to test Firefox using an extension. I want to automate the test and visit several websites.
I installed Selenium and it opens in geckodriver. However, the extension is not there. I can install it manually from about:debugging but the problem is that I want the Selenium test to launch the gecko driver while the extension is already there. How to do this? How to install the extension permanently in the geckodriver so it is there when I launch the geckodriver from selenium?
EDIT:
I also tried to install the extension (add it to the browser) from the Firefox extensions websites. It gets added but once I close the gecko window, the extension disappear in the next run. How to install it permanently?
Note: OP didn't specify a language, so this answer is for Python. The other Selenium WebDriver language bindings have similar mechanisms for creating profiles and adding extensions.
You can install the Extension each time you instantiate the driver.
First, download the extension (XPI file) you want from: https://addons.mozilla.org.
Then, in your code... create a FirefoxProfile() and use the add_extension() method to add the extension. Then you can instantiate a driver using that profile.
For example, this will launch Firefox with a newly created profile containing the "HTTPS Everywhere" extension:
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.add_extension(extension='https_everywhere-2019.1.31-an+fx.xpi')
driver = webdriver.Firefox(firefox_profile=profile)
You need to launch geckdriver with an exisitng profile by specifying the profile path of firefox
For python you can do it by this:
profile = FirefoxProfile('/home/student/.mozilla/firefox/gwi6uqpe.Default') // change this path
browser = webdriver.Firefox(firefox_profile=profile)
For C# you can do this:
string path = #"C:\Users\username\AppData\Local\Mozilla\Firefox\Profiles\myi5go1k.default";
FirefoxProfile ffprofile = new FirefoxProfile(path);
Driver = new FirefoxDriver(ffprofile);
I found that this was warking for me:
from selenium import webdriver
driver_path = r"G:\Libs\geckoDriver\firefox\geckodriver.exe"
driver = webdriver.Firefox(executable_path=driver_path)
path = r"G:\Libs\ext\uBlock0_1.38.7b5.firefox.signed.xpi"
driver.install_addon(path, temporary=True)
driver.profile = webdriver.FirefoxProfile()
driver.profile.add_extension(path)
driver.profile.set_preference("security.fileuri.strict_origin_policy", False)
driver.profile.update_preferences()`enter code here`
Reference:
[Python] https://cyruslab.net/2020/08/26/python-adding-extension-to-geckodriver-with-selenium/
You can install an extension/addon permanently within a specific Firefox Profile and use it. To achieve that you need follow the below mentioned steps:
You need to create a new Firefox Profile manually (e.g. FirefoxExtensionProfile) following the instructions at Creating a new Firefox profile on Windows.
Open a Firefox Browsing session manually and invoke the url https://addons.mozilla.org/en-US/firefox/
In the Search Box search for an extension e.g. HTTPS Everywhere.
Click on the search result and install / enable (incase previously installed and currently disabled) the extension.
Now you can use the following Java solution to open the Firefox Profile FirefoxExtensionProfile containing the extension HTTPS Everywhere
Code Block:
package A_MozillaFirefox;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.ProfilesIni;
public class A_FirefoxProfile_dc_opt {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("FirefoxExtensionProfile");
FirefoxOptions opt = new FirefoxOptions();
opt.setProfile(testprofile);
WebDriver driver = new FirefoxDriver(opt);
driver.get("https://www.google.com");
}
}
Browser Snapshot:
Reference
You can find a couple of relevant discussions in:
[Python] How to load extension within chrome driver in selenium with python
[Python] How to install Chrome Extension using Selenium & Python

Browser not launching at all using selenium java

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments(Arrays.asList("--start-maximized", "allow-running- insecure-content", "ignore-certificate-errors")); capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver webDriver = new RemoteWebDriver(new URL("http://www.google.com"), capabilities);
webDriver.findElement.....
I have the above piece of code to start a browser and go to a URL. I did a
1. brew install chromedriver
2. i made sure /usr/local/bin is in the path variable.
3. When i run the above peice of code it fails in find Element giving a null pointer exception.
I am running mac os.. how can i fix the problem. i did lot of work arounds, the same code without capabilities and just giving the chromedriver path was working before.. not sure why i am not able to get it to run..
There are 2 solutions to your problem:
Make sure you want to use RemoteWebDriver. If you do you will want to set up a Selenium Grid with a chrome node. I won't describe how to set it up here, but it is fairly easy to set up a local grid+nodes using docker: https://github.com/SeleniumHQ/docker-selenium
If you do use this, you will need to change the driver to (assuming the grid is on the localhost):
String hubURL = "http://localhost:4444/wd/hub";
WebDriver webDriver = RemoteWebDriver(new URL(hubURL), capabilities);
Or Use the ChromeDriver class instead of RemoteWebDriver:
WebDriver webDriver = new ChromeDriver(capabilities);
Finally, to go to a webpage, you'll need to use the get method on the driver:
webDriver.get(url);
Updating to the latest chrome driver fixed the issue.

Selenium test stuck on starting using chromeDriver

I am trying to write a selenium test with fluintlenium and for some reason when I try using the chrome driver it just open chrome on "data;" page and doesn't continue to the actual url I give it.
When I use HtmlUnitDriver it runs the test successfully but with chrome it just stuck after :
Starting ChromeDriver 2.15.322448 (****) on port 36888
Only local connections are allowed.
I know its just an information message but it doesnt continue after that just stuck it doesn't show any errors...
The code is:
public class IntegrationTest extends FluentTest {
public WebDriver driver;
#Override
public WebDriver getDefaultDriver() {
driver = new ChromeDriver();
return driver;
}
/**
* add your integration test here
*/
#Test
public void title_of_bing_should_contain_search_query_name() {
goTo("http://www.bing.com");
fill("#sb_form_q").with("FluentLenium");
submit("#sb_form_go");
assertThat(title()).contains("FluentLenium");
}
}
I tried using selenium's driver.get("url"); but its not working either .
EDIT : How I run the tests? 1. I just start my selenium server 2. I use play so I run the command play test OR 2. I use eclipse junit test runner to run a single test.
Both doesn't work ...
It doesn't mean the chromedriver is not launched. It's just an information message that you can connect to it only locally.
If the browser opened and not doing anything further, in your case it means you have not compatible chrome and chrome driver. If your chrome is the latest one, then 2.13 chromedriver is too old for you, try 2.15

Selenium. IEDriverServer.exe does not exist?

I get an error:
The file Capabilities [BrowserName=, IsJavaScriptEnabled=False, Platform=Any, Version=]\IEDriverServer.exe does not exist. The driver can be downloaded at http://code.google.com/p/selenium/downloads/list`
What must I do to solve this error because really I can't find information ....
Unless you're building the Selenium project from sources, or are using the .NET bindings and have set a specific option in the InternetExplorerOptions class, you shouldn't be seeing this message. The current source code is volatile in this particular area, as the project is currently implementing the usage of a standalone executable server for the IE driver, similar to what happens currently for the Chrome driver. If you are building from source, or are interested in using this new functionality, you should download the standalone IE driver server executable from the URL specified in the exception's message.
Download the IEDriver.exe for the given URL
http://code.google.com/p/selenium/downloads/list
Assuming the exe file is saved in E:\IEdriver.exe.
In Selenium you have to set the property of IEDriver, since Selenium has only firefox driver with it.
Set the system property and then invoke the IEDriver.
WebDriver driver;
System.setProperty("webdriver.ie.driver", "E:\\IEdriver.exe");
//Selenium will not support single slash, so we are providing double slash in path.
driver = new InternetExplorerDriver();
// By this it will call the IEDriver and execute
Thanks
An example to share:
public static void main(String[] args) {
System.setProperty("webdriver.ie.driver", "D:\\selenium\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("www.google.com");
driver.findElement(By.id("gbqfq")).sendKeys("abc");
driver.close();
}
}