Selenium Python firefox webdriver - selenium

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')

Related

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

Selenium driver plugin using Java

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);

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.

Jmeter with selenium webdriver plugin-start firefox session without cleaning cookies/cache

I am using Jmeter 2.13 alongside with a selenium webdriver plugin. When I start my thread it opens a new firefox session with all the cookies and cache cleared. In a previous session I have made a sync that lasts almost 5 minutes that brings me in my app some products. I don`t want to sync everytime I start the thread.
Is there any way I could start the new firefox session without clearing cookies/cache ?
If my guess is correct, that Selenium is the one that is opening an instance of Firefox, please see below. If not, please provide more info about what you have setup with code examples.
By default Selenium is opening the Firefox in Safe mode, where a lot of stuff are disabled (like extensions, localstore settings, etc). This also means that you won't have any cache or cookies.
You can read more about this here.
To disable this, you need to set the `toolkit.startup.max_resumed_crashes` setting key in in `about:config` to `-1`.
Selenium code wise, this can be achieved by setting the preference in the FirefoxProfile. C# code to achieve it would look like this:
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.SetPreference("toolkit.startup.max_resumed_crashes", "-1");
IWebDriver driver = new FirefoxDriver(firefoxProfile);
Looking into FirefoxDriverConfig.java source code the plugin creates new profile each time Firefox starts:
FirefoxProfile createProfile() {
FirefoxProfile profile = new FirefoxProfile();
String userAgentOverride = getUserAgentOverride();
String ntlmOverride = getNtlmSetting();
if (StringUtils.isNotEmpty(userAgentOverride)) {
profile.setPreference("general.useragent.override", userAgentOverride);
}
if (StringUtils.isNotEmpty(ntlmOverride)) {
profile.setPreference("network.negotiate-auth.allow-insecure-ntlm-v1", true);
}
profile.setPreference("app.update.enabled", false);
addExtensions(profile);
setPreferences(profile);
return profile;
}
So there are 2 options:
Get plugin source code and amend profile initialisation line to use your existing profile as:
FirefoxProfile profile = new FirefoxProfile("/path/to/firefox/profile");
See How do I find my profile page of Mozilla documentation for instructions on how you can locate your current profile directory.
Stop using WebDriver Sampler and switch to JSR223 Sampler instead, it supports all the languages WebDriver Sampler does and provides full control (you'll have to write all the code to configure, start and stop browser yourself)

How to create profile in Firefox using Selenium WebDriver

When we write something like this:
FirefoxProfile ffprofile = new FirefoxProfile(new File("D:\\Selenium"));
Does it mean we are creating a new Profile? Because I won't be able to find any new profile in the Firefox Profile section.
So now my question is, how do I create a new profile for a Firefox browser?
The method call you stated simply creates a java profile object from the given directory of profile information which is then passed to Firefox via the WebDriver instance.
In order to get Firefox to persist your driver and make it available from profile manager, you need to edit the file profiles.ini, on my (Windows 7) machine this was in:
%APPDATA%\Roaming\Mozilla\Firefox
The Profiles directory within this folder contains the stores of existing Firefox profiles, which are quite handy to copy when you want to use an existing profile as the template for a new one.
Your mileage may vary depending on your OS, but I'm sure you can find it with a quick search. Using your example, you'd then add the following to this file (where N in the header is the next unused profile number):
[ProfileN]
Name=selenium
IsRelative=0
Path=D:\Selenium
This will cause Firefox Profile Manager to load the profile and will allow you to then launch Firefox manually with this profile to configure or test it, which is what I presume you want to do.
Once you have created a named profile in this way, you can assign it to your driver in Selenium like this:
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("selenium");
WebDriver driver = FirefoxDriver(profile);
Where "selenium" is the same as the Name property in the profiles.ini file.
You cannot create a profile for firefox using Selenium. What you can do is create a firefox profile for your webdriver from the available profiles in firefox. Firefox profile word sounds bit ambiguous here.
To create a firefox profile in browser, refer Mozilla support page for details.
Following code will create firefox profile (based on provided file) and create a new FF webdriver instance with this profile loaded:
FirefoxProfile profile = new FirefoxProfile(new File("D:\\Selenium Profile"));
WebDriver driver = new FirefoxDriver(profile);
Maybe take a look on the official support page for FF profile manager
or here: Custom Firefox profile for Selenium to get some idea on FF profiles.
Here is how I do by selenium 3 with geckodriver:
Use firefox command line interface to create profile
firefox.exe -CreateProfile "profile_name profile_dir"
(In java, execute this runtime by Runtime.getRuntime().exec function)
Set -profile argument in firefox options
FirefoxOptions options = new FirefoxOptions();
options.addArguments("-profile", <profile_dir>);
driver = new FirefoxDriver(options);
Create profile in Firefox browser.
Here is the code for using newly created firefox profile.
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("firefox profile name");
WebDriver driver = new FirefoxDriver(myprofile);