使用自定义配置文件的Selenium 2 WebDriver

4
我正在尝试自动化与生成MIME类型为application/vnd.wap.xhtml+xml的文档的网站进行交互。我正在使用Selenium 2,WebDriver和FirefoxProfile。
由于Firefox不能处理上述MIME类型,因此我需要使用XHTML Mobile Profile扩展来运行Firefox (https://addons.mozilla.org/en-US/firefox/addon/1345/)。
创建了一个名为“selenium”的FireFox配置文件并安装了Mobile Profile扩展后,我尝试使用“Selenium 2.0 and WebDriver”文档的“Tips and Tricks”部分中的代码片段(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"));

方法二如下:

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 用户组帖子链接

也许你最好去问一下Selenium支持论坛? - Peter Lawrey
5
我一定会做到。感谢你的建议。StackOverflow社区太令人印象深刻了,它已经成为我所有问题的第一站... :) - Ytsejammer
你能验证在Selenium启动时Firefox是否加载了插件吗?(例如,确保测试不会关闭它启动的浏览器,然后查看插件是否出现在添加窗口中。如果没有,则可以尝试一些方法:将您的配置文件命名为Selenium以外的其他名称(以防Selenium调用其自己创建的默认配置文件),或者尝试使用Java等效的add_extension方法,该方法由Ruby绑定提供给Firefox配置文件,以自动将扩展加载到Selenium的配置文件中。) - Andy Tinkham
如果您已经添加了,请在Selenium支持论坛中添加问题的直接链接...。这个问题对我非常重要,答案也是如此 :) - Jesper Rønn-Jensen
完成! :) 不幸的是,这里或Selenium用户组都没有提供答案。祝你好运! - Ytsejammer
3个回答

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);
}

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();

如何修改标题?可以用“修改”或“删除”代替“添加”吗? - Nathan B

1

你需要确保将浏览器插件作为DeploymentItem添加到你的testsettings文件中。 以下是一些示例(在这个示例中,我们添加了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驱动程序中可用。希望这能帮到您。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接