首页 文章

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

提问于
浏览
4

我正在尝试自动化与生成MIME类型为application / vnd.wap.xhtml xml的文档的网站的交互 . 我正在使用Selenium 2,WebDriver和FirefoxProfile .

由于Firefox不处理上述MIME类型,因此我需要使用XHTML Mobile Profile扩展(https://addons.mozilla.org/en-US/firefox/addon/1345/)运行Firefox .

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

方法#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类型的内容,就好像扩展名未正确配置一样 .

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

提前致谢,

EditLink to Selenium users group post .

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();
    
  • 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驱动程序中可用 . 我希望这有帮助 .

相关问题