定制自己的firefox插件並在selenium中使用


首先搭建火狐插件開發環境:

然后編寫一個簡單的firefox插件

  • 先創建一個火狐插件工作空間,比如:d:\ff-addon-ws
  • 然后cd到"火狐sdk/bin"目錄下,再打開一個新的cmd窗口,然后把activate這個文件拖到cmd窗口中運行
  • 然后cd到火狐插件工作空間,
  • 創建一個插件項目並初始化
  • 項目創建完成后編寫插件具體實現,這里直接拷貝一段簡單的代碼.具體做法是進入剛剛創建的插件項目目錄下的lib子文件夾,里面有一個main.js,然后把下面這段代碼直接拷進去
    var buttons = require('sdk/ui/button/action');
    var tabs = require("sdk/tabs");
    
    var button = buttons.ActionButton({
      id: "mozilla-link",
      label: "Visit Mozilla",
      icon: {
        "16": "./icon-16.png",
        "32": "./icon-32.png",
        "64": "./icon-64.png"
      },
      onClick: handleClick
    });
    
    function handleClick(state) {
      tabs.open("https://www.mozilla.org/");
    }
    
  • 由於這段代碼使用了3張圖片,所以在本文附加中下載下來然后放到插件目錄下的data子文件夾中
  • 完成上面幾個步驟之后就可以測試這個插件了,輸入cfx run,如圖:
  • 點擊右邊火狐小圖標后會打開一個新的標簽頁轉到火狐主頁,到這里一個最簡單的定制插件就完成啦!接下來就是打包成xpi,這樣就可以安裝到火狐瀏覽中

插件編寫參考自:https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials/Getting_started

最后一步就是在selenium中使用了,沒用過selenium的朋友可以不看下面的

		System.setProperty("webdriver.firefox.bin",
				"D:/Program Files (x86)/Mozilla Firefox/firefox.exe");
		TemporaryFilesystem.setTemporaryDirectory(new File(
				"D:/workspace/firefox/seleniumTemp/"));
		FirefoxProfile firefoxProfile = new FirefoxProfile(new File(
				"D:/workspace/firefox/profiles/test/"));
		File cookieWriter = new File("rwfile.xpi");
		try {
			firefoxProfile.addExtension(cookieWriter);
			//設置插件相關參數  參數格式為   extensions.插件名.參數名
			// 設置插件版本
			firefoxProfile.setPreference("extensions.rwfile.currentVersion",
					"1.0");
			//默認情況下通過selenium加載的插件是被火狐禁用的,設置下面幾個參數就可以啟用了
			firefoxProfile.setPreference(
					"extensions.rwfile.console.enableSites", true);
			firefoxProfile.setPreference("extensions.rwfile.net.enableSites",
					true);
			firefoxProfile.setPreference(
					"extensions.rwfile.script.enableSites", true);
			firefoxProfile.setPreference(
					"extensions.rwfile.allPagesActivation", "on");
		} catch (IOException e) {
			e.printStackTrace();
		}
		WebDriver driver = new FirefoxDriver(firefoxProfile);
		driver.get("http://www.xhalj.com/");
	

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM