4

我正在尝试自动与生成具有 MIME 类型 application/vnd.wap.xhtml+xml 的文档的网站进行交互。我正在使用 Selenium 2、WebDriver 和 FirefoxProfile。

因为 Firefox 不处理上述 MIME 类型,我需要使用 XHTML 移动配置文件扩展 (https://addons.mozilla.org/en-US/firefox/addon/1345/) 运行 Firefox。

创建 FireFox 配置文件(我将其命名为“selenium”)并安装 Mobile Profile 扩展后,我尝试使用“Selenium 2.0 和 WebDriver”文档(http://seleniumhq)的“提示和技巧”部分中的代码片段.org/docs/09_webdriver.html#htmlunit-driver)。

方法 #1 如下所示:

ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("selenium");
profile.setPreference("general.useragent.override", "User Agent string to force application/vnd.wap.xhtml+xml content..");
FirefoxDriver driver = new FirefoxDriver(profile);
driver.get("http://www.mobilesite.com/");
WebElement element = driver.findElement(By.tagName("body"));

方法 #2 如下所示:

File profileDir = new File("/path/to/custom/profile/with/extension/ffprofile");
FirefoxProfile profile = new FirefoxProfile(profileDir);
profile.setPreference("general.useragent.override", "same user agent string as above");
FirefoxDriver driver = new FirefoxDriver(profile);
driver.get("http://www.mobilesite.com/");

无论我使用什么代码片段,启动的浏览器实例总是无法处理生成的内容;浏览器提示我对无法识别的 MIME 类型的内容执行操作,就像未正确配置扩展一样。

关于我可能做错了什么的任何想法?

提前致谢,

编辑链接到 Selenium 用户组帖子

4

3 回答 3

1

希望这会对您有所帮助:

public class Wap {

public static void main (String[] args) throws IOException{ 

FirefoxProfile profile = new FirefoxProfile();
String baseURL;
profile.addExtension(new File("C:\\Users\\Pandu\\Desktop\\WAP\\modify_headers-0.7.1.1-fx.xpi"));

profile.setPreference("modifyheaders.config.active", true);
profile.setPreference("modifyheaders.config.alwaysOn", true);
profile.setPreference("modifyheaders.headers.count", 2);
profile.setPreference("modifyheaders.headers.action0", "Add");
profile.setPreference("modifyheaders.headers.name0", "X-Nokia-msisdn");
profile.setPreference("modifyheaders.headers.value0", "123456789");
profile.setPreference("modifyheaders.headers.enabled0", true);
profile.setPreference("modifyheaders.headers.action1", "Add");
profile.setPreference("modifyheaders.headers.name1", "x-sec-pass");
profile.setPreference("modifyheaders.headers.value1", "sdp123");
profile.setPreference("modifyheaders.headers.enabled1", true);


    Logger Log = Logger.getLogger(WebDriver.class.getName());

    WebDriver driver = new FirefoxDriver(profile);
    try{
driver.get("http://www.google.com");

        driver.findElement(By.linkText("Telugu")).click();
于 2012-02-08T03:02:06.627 回答
1

尝试从空白配置文件开始并在运行时添加扩展/配置:

public WebDriver getDriver() {
    FirefoxProfile profile = new FirefoxProfile();

    // add any custom firefox configurations...
    profile.setPreference("general.useragent.override", "some UA string");
    profile.setPreference("javascript.options.showInConsole", true);
    profile.setPreference("dom.max_script_run_time", 0);

    // might have to uninstall, search for *.xpi, then reinstall, then search 
    // again and compare to find the location on your system
    // ...you should probably copy this into your selenium resources directory!
    File modifyHeadersXpi = new File("/home/joecoder/.mozilla/firefox/dll8peh9.default/extensions/{b749fc7c-e949-447f-926c-3f4eed6accfe}.xpi");
    if (modifyHeadersXpi.exists()) {
        try {
            profile.addExtension(modifyHeadersXpi);
            profile.setPreference("modifyheaders.config.active", true);
            profile.setPreference("modifyheaders.config.openNewTab", true);
            profile.setPreference("modifyheaders.config.migrated", true);
            profile.setPreference("modifyheaders.autocomplete.name.defaults", 
                    "[\"Accept\",\"Cache-Control\",\"Cookie\",\"Content-Length\",\"Content-Type\",\"Date\",\"Host\",\"Pragma\",\"Referer\",\"User-Agent\",\"Via\",\"X-Requested-With\",\"X-Forwarded-For\",\"X-Do-Not-Track\"]");
        }
        catch (IOException e) { /* uh oh */ }
    }
    return new FirefoxDriver(profile);
}
于 2012-02-05T01:29:12.420 回答
1

您必须确保在 testsettings 文件中将浏览器插件添加为 DeploymentItem。一些示例(在这一示例中,我们添加了 Firebug):

  <Deployment>
    <DeploymentItem filename="Selenium\firebug@software.joehewitt.com.xpi" />
    <DeploymentItem filename="packages\Castle.Core.3.1.0\lib\net40-client\Castle.Core.dll" />
    <DeploymentItem filename="Selenium\IEDriverServer.exe" />
    <DeploymentItem filename="Selenium\chromedriver.exe" />
    <DeploymentItem filename="Selenium\skipcerterror@foudil.fr.xpi" />
  </Deployment>

然后,您需要创建一个如下所示的配置文件:

string firebugPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? "", "firebug@software.joehewitt.com.xpi");

FirefoxProfile firebugProfile = new FirefoxProfile() {AcceptUntrustedCertificates = true};
firebugProfile.AddExtension(firebugPath);
firebugProfile.SetPreference("extensions.firebug.currentVersion", "1.10.3");
firebugProfile.SetPreference("extensions.sce.bypass_domain_mismatch", true);
firebugProfile.SetPreference("webdriver_assume_untrusted_issuer", false);

Driver = new FirefoxDriver(firebugProfile);
Driver.Manage().Window.Maximize();

如果您使用 AddExtension 添加扩展,它应该在您的 selenium 驱动程序中可用。我希望这有帮助。

于 2013-01-31T00:23:25.393 回答