如何从 Selenium 脚本获取 Firefox Web Extension 的 "Internal UUID"?
How to get Firefox Web Extension's "Internal UUID" from Selenium script?
我需要在我的 Selenium 脚本启动后打开 moz-extension://internal-uuid
页面以访问扩展程序的 storage API
,在那里设置一些首选项,这个扩展程序稍后会读取并用来做一些动作。但是当我使用 selenium.webdriver.Firefox.add_addon(...)
时,它 returns 与 Extension ID
不同并且不能用于打开 moz-extension://
页面。有什么方法可以从我的代码中获取此 Internal UUID
(不是通过检查 about:debugging#addons
手动获取)。或者可以通过某种方式将我需要的数据从 Selenium 传递到 Web Extension?
此代码在 Linux 和 Mac 中对我有效:
public static void main(String[] args) throws IOException {
FirefoxOptions options = new FirefoxOptions();
FirefoxDriver driver = new FirefoxDriver(options);
String userPrefsFileContent = readFile(driver.getCapabilities().getCapability("moz:profile") + "/prefs.js");
String extensionUuid = getExtensionUuid(userPrefsFileContent);
driver.quit();
}
private static String getExtensionUuid(String userPrefsFileContent) {
String uuid = null;
String[] usersPrefsList = userPrefsFileContent.split(";");
for (String currentPref : usersPrefsList) {
if (currentPref.contains("extensions.webextensions.uuids")) {
uuid = currentPref.split(":")[1].replaceAll("\"", "").replace("}", "")
.replace(")", "").replace("\", "");
}
}
if(uuid.contains(",")) {
uuid = uuid.split(",")[0];
}
return uuid;
}
private static String readFile(String pathname) throws IOException {
File file = new File(pathname);
StringBuilder fileContents = new StringBuilder((int) file.length());
String lineSeparator = System.getProperty("line.separator");
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
fileContents.append(scanner.nextLine()).append(lineSeparator);
}
}
return fileContents.toString();
}
不知道如何,但我无法在 python 中使用这种方法,所以找到了另一种聪明的方法,而不是尝试获得 Firefox 未提供的首选项,您可以设置set-preference
API 的 UUID。然后就用它。
这是我的部分代码
if self.webdriver != None:
return self.webdriver
extensionPath = curPath+'/../../packages/firefox.xpi';
cap = DesiredCapabilities().FIREFOX
cap["marionette"]=True;
profile = webdriver.firefox.firefox_profile.FirefoxProfile();
profile.set_preference('extensions.webextensions.uuids',json.dumps({'support@ipburger.com':firefoxUUID}))
self.webdriver = webdriver.Firefox(firefox_binary=firefoxBinary,capabilities=cap,executable_path=curPath+'/../../drivers/geckodriver',firefox_profile=profile);
self.webdriver.install_addon(extensionPath,temporary=True);
return self.webdriver;
firefoxUUID 是您生成的随机 UUID 的字符串版本
你必须用你的插件 ID 替换 support@ipburger.com
,你可以在 manifest.json 文件
中设置它
下面的代码在 windows 的 python 中对我有用:
driver = webdriver.Firefox(service=firefox_service, options=firefox_options)
driver.install_addon(os.path.join(application_path, 'extension_firefox.xpi'), temporary=True)
time.sleep(1)
profile_path = driver.capabilities['moz:profile']
with open('{}/prefs.js'.format(profile_path), 'r') as file_prefs:
lines = file_prefs.readlines()
for line in lines:
if 'extensions.webextensions.uuids' in line:
sublines = line.split(',')
for subline in sublines:
if id_extension_firefox in subline:
internal_uuid = subline.split(':')[1][2:38]
我需要在我的 Selenium 脚本启动后打开 moz-extension://internal-uuid
页面以访问扩展程序的 storage API
,在那里设置一些首选项,这个扩展程序稍后会读取并用来做一些动作。但是当我使用 selenium.webdriver.Firefox.add_addon(...)
时,它 returns 与 Extension ID
不同并且不能用于打开 moz-extension://
页面。有什么方法可以从我的代码中获取此 Internal UUID
(不是通过检查 about:debugging#addons
手动获取)。或者可以通过某种方式将我需要的数据从 Selenium 传递到 Web Extension?
此代码在 Linux 和 Mac 中对我有效:
public static void main(String[] args) throws IOException {
FirefoxOptions options = new FirefoxOptions();
FirefoxDriver driver = new FirefoxDriver(options);
String userPrefsFileContent = readFile(driver.getCapabilities().getCapability("moz:profile") + "/prefs.js");
String extensionUuid = getExtensionUuid(userPrefsFileContent);
driver.quit();
}
private static String getExtensionUuid(String userPrefsFileContent) {
String uuid = null;
String[] usersPrefsList = userPrefsFileContent.split(";");
for (String currentPref : usersPrefsList) {
if (currentPref.contains("extensions.webextensions.uuids")) {
uuid = currentPref.split(":")[1].replaceAll("\"", "").replace("}", "")
.replace(")", "").replace("\", "");
}
}
if(uuid.contains(",")) {
uuid = uuid.split(",")[0];
}
return uuid;
}
private static String readFile(String pathname) throws IOException {
File file = new File(pathname);
StringBuilder fileContents = new StringBuilder((int) file.length());
String lineSeparator = System.getProperty("line.separator");
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
fileContents.append(scanner.nextLine()).append(lineSeparator);
}
}
return fileContents.toString();
}
不知道如何,但我无法在 python 中使用这种方法,所以找到了另一种聪明的方法,而不是尝试获得 Firefox 未提供的首选项,您可以设置set-preference
API 的 UUID。然后就用它。
这是我的部分代码
if self.webdriver != None:
return self.webdriver
extensionPath = curPath+'/../../packages/firefox.xpi';
cap = DesiredCapabilities().FIREFOX
cap["marionette"]=True;
profile = webdriver.firefox.firefox_profile.FirefoxProfile();
profile.set_preference('extensions.webextensions.uuids',json.dumps({'support@ipburger.com':firefoxUUID}))
self.webdriver = webdriver.Firefox(firefox_binary=firefoxBinary,capabilities=cap,executable_path=curPath+'/../../drivers/geckodriver',firefox_profile=profile);
self.webdriver.install_addon(extensionPath,temporary=True);
return self.webdriver;
firefoxUUID 是您生成的随机 UUID 的字符串版本
你必须用你的插件 ID 替换 support@ipburger.com
,你可以在 manifest.json 文件
下面的代码在 windows 的 python 中对我有用:
driver = webdriver.Firefox(service=firefox_service, options=firefox_options)
driver.install_addon(os.path.join(application_path, 'extension_firefox.xpi'), temporary=True)
time.sleep(1)
profile_path = driver.capabilities['moz:profile']
with open('{}/prefs.js'.format(profile_path), 'r') as file_prefs:
lines = file_prefs.readlines()
for line in lines:
if 'extensions.webextensions.uuids' in line:
sublines = line.split(',')
for subline in sublines:
if id_extension_firefox in subline:
internal_uuid = subline.split(':')[1][2:38]