How to install extension permanently in geckodriver

旧巷老猫 提交于 2019-11-26 18:35:15

问题


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?


回答1:


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) 



回答2:


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



回答3:


You can install an extension 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:



来源:https://stackoverflow.com/questions/54754945/how-to-install-extension-permanently-in-geckodriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!