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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4599763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:09:12  来源:igfitidea点击:

Selenium 2 WebDriver to use Custom Profile

javatestingseleniumfirefox3.6

提问by Ytsejammer

I'm trying to automate the interaction with a website that generates documents with MIME type application/vnd.wap.xhtml+xml. I am using Selenium 2, the WebDriver and the FirefoxProfile.

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

Because Firefox does not handle the above mentioned MIME type, I need to run Firefox with the XHTML Mobile Profile extension (https://addons.mozilla.org/en-US/firefox/addon/1345/).

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

After creating a FireFox profile -I named it 'selenium'- and installing the Mobile Profile extension, I tried to use the code snippets in the 'Tips and Tricks' section of the 'Selenium 2.0 and WebDriver' document (http://seleniumhq.org/docs/09_webdriver.html#htmlunit-driver).

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

Approach #1 looks like this:

方法#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"));

Approach #2 looks like this:

方法#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/");

No matter what code snippet I use, the browser instance that starts up is always unable to handle the generated content; the browser prompts me for an action to take on the content of the unrecognized MIME type as if the extension was not correctly configured.

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

Any ideas on what I could be doing wrong?

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

Thanks in advance,

提前致谢,

Edit: Link to Selenium users group post.

编辑链接到 Selenium 用户组帖子

回答by Joe Coder

Try starting with a blank profile and adding extensions/configurations at runtime:

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

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

回答by Naresh

Hope this will help you out:

希望这会帮助你:

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

回答by Michiel Bugher

You have to make sure you add the browser plugin as a DeploymentItem in your testsettings file. Some examples (in this one we have added Firebug):

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

  <Deployment>
    <DeploymentItem filename="Selenium\[email protected]" />
    <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\[email protected]" />
  </Deployment>

You will then need to create a profile that looks something like this:

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

string firebugPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? "", "[email protected]");

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

If you add the extension using AddExtension, it should be available within you selenium driver. I hope this helps.

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