问题
I want to install a custom XPI file in Firefox when running it with selenium
and geckodriver
in a TypeScript and Jest context.
The important part of the test script is this:
let driver: webdriver.WebDriver;
const firefoxExt = path.resolve(__dirname, '..', '..', 'extension', 'firefox.xpi');
const firefoxOptions = new firefox.Options().addExtensions(firefoxExt);
driver = new webdriver.Builder().forBrowser('firefox').setFirefoxOptions(firefoxOptions).build();
I am expecting Firefox to launch and install firefox.xpi as an add-on, but there are no add-ons present in the opened Firefox instance. There is no issue with the XPI itself, as the XPI can be installed manually, as a temporary extension, without issues. Also, the XPI exists at the path, as otherwise it would error out on path.resolve
.
For others to reproduce the issue, I have created a repository with a minimal, reproducible example. See this repo: https://github.com/slhck/web-extension-selenium-test
Note that this is not a duplicate of:
- Cannot load extension using firefox driver selenium when using AddExtension (unanswered, closed)
- How to install extension permanently in geckodriver (I do not want a permanent install, as the XPI will be changed before the test suite runs)
- How to load extension within chrome driver in selenium with python (I am already using
addExtension
)
I have created a bug report in Selenium itself, but it has not received any activity yet.
Does anyone know what the issue could be, and how it could be solved?
回答1:
You can use installAddon
. There may be a more elegant way to do this, but the code below works:
beforeAll(async () => {
const firefoxExt = path.resolve(__dirname, '..', '..', 'extension', 'firefox.xpi');
driver = new webdriver.Builder().forBrowser('firefox').build();
new firefox.Driver(driver.getSession(), driver.getExecutor()).installAddon(firefoxExt, true);
});
来源:https://stackoverflow.com/questions/61851866/selenium-does-not-install-add-on-in-firefox-when-using-the-addextensions-option