0

如何将修改标头添加到 fifrefox 配置文件并在用户代理中运行测试值:Mozilla/5.0(iPhone;U;CPU iPhone OS 4_0,如 Mac OS X;en-us)AppleWebKit/532.9(KHTML,如 Gecko)移动/7D11。为此,我使用了以下代码 fp 是 firefox

profile object fp.setEnableNativeEvents(true);                  
File file = new File("C:\\FirefoxProfile\\modify_headers-0.7.1.1-fx.xpi");
//File file = new File("C:\\AddOn");
fp.addExtension(file);
fp.setPreference("extensions.modify_headers.currentVersion", "0.7.1.1-fx");
fp.setPreference("modifyheaders.config.alwaysOn", true);
fp.setPreference("modifyheaders.headers.count", 2);
fp.setPreference("modifyheaders.headers.action1", "Modify");
fp.setPreference("modifyheaders.headers.name0", "User-Agent");
fp.setPreference("modifyheaders.headers.value0", "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Mobile/7D11");
fp.setPreference("modifyheaders.headers.enabled0", true);                   
4

2 回答 2

3

如果您真的热衷于使用 Modify 标头:

  1. 确保 Modify-header 包含在您的项目中,并且可以访问

public static void main(String[] args) { File modifyHeaders = new File(System.getProperty("user.dir") + "/src/test/resources/modify_headers-0.7.1.1-fx.xpi");

FirefoxProfile profile = new FirefoxProfile();
try {
    profile.setEnableNativeEvents(false);
    profile.addExtension(modifyHeaders);
    profile.setPreference("extensions.modify_headers.currentVersion", "0.7.1.1-fx");
    profile.setPreference("modifyheaders.headers.count", 1);
    profile.setPreference("modifyheaders.headers.action0", "Add");
    profile.setPreference("modifyheaders.headers.name0", "User-Agent");
    profile.setPreference("modifyheaders.headers.value0", "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Mobile/7D11");
    profile.setPreference("modifyheaders.headers.enabled0", true);
    profile.setPreference("modifyheaders.config.active", true);
    profile.setPreference("modifyheaders.config.alwaysOn", true);
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setBrowserName("firefox");
    capabilities.setPlatform(org.openqa.selenium.Platform.ANY);
    capabilities.setCapability(FirefoxDriver.PROFILE, profile);
    WebDriver driver = new FirefoxDriver(capabilities);

    driver.get("https://google.com.au");
} catch (IOException e) {
    e.printStackTrace();
}
// .... the rest}
于 2014-09-30T01:53:03.853 回答
1

修改用户代理的方法比使用附加组件要简单得多。您需要做的就是为 Firefoxgeneral.useragent.override设置分配一个新值。

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("general.useragent.override", "My new shiny user agent");     
WebDriver driver = new FirefoxDriver(profile);

driver.get("http://www.whatsmyuseragent.com/");
于 2013-10-15T16:49:33.420 回答