如何在WebDriver测试中更改Firefox设置?

2 投票
1 回答
3510 浏览
提问于 2025-04-18 08:41

我在用Python在Firefox上运行WebDriver测试。我已经设置好Firefox,让所有社交网站的链接都在当前标签页打开。我特别做了以下两个修改:

browser.link.open_newwindow.restriction then,  change the value to 0 (zero)
browser.link.open_newwindow and change the value to 1 (one)

你可以在这个链接找到详细信息:https://support.mozilla.org/en-US/questions/970999

我的WebDriver Firefox设置包括:

from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.common.action_chains import ActionChains

success = True
wd = WebDriver()
wd.implicitly_wait(60)

我该如何在开始测试代码之前,把这些设置也加到上面的配置中呢?

补充说明

当我尝试更改browser.link.open_newwindow的值时,出现了以下错误:

Exception in thread "main" java.lang.IllegalArgumentException: Preference     browser.link.open_newwindow may not be overridden: frozen value=2, requested value=1
    at com.google.common.base.Preconditions.checkArgument(Preconditions.java:120)
    at org.openqa.selenium.firefox.Preferences.checkPreference(Preferences.java:223)
    at org.openqa.selenium.firefox.Preferences.setPreference(Preferences.java:161)
    at org.openqa.selenium.firefox.FirefoxProfile.setPreference(FirefoxProfile.java:230)
    at tmp.main(tmp.java:21)

1 个回答

3

你可以通过一个配置文件来设置偏好设置,像这样。

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.link.open_newwindow.restriction", 0);
profile.setPreference("browser.link.open_newwindow", 1);
WebDriver webDriver =  new FirefoxDriver(profile);

虽然我现在无法测试,但Python代码大概会是这样的。

from selenium import webdriver
profile = webdriver.FirefoxProfile();
profile.set_preference("browser.link.open_newwindow.restriction", 0);
profile.set_preference("browser.link.open_newwindow", 1);
wd =  webdriver.Firefox(profile);

来源:FirefoxDriver 小贴士与技巧

撰写回答