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

发布于 2024-10-10 12:37:04 字数 1565 浏览 4 评论 0原文

我正在尝试自动与生成 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/)。

创建 FireFox 配置文件(我将其命名为“selenium”)并安装移动配置文件扩展后,我尝试使用“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 用户群组帖子的链接

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.

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/).

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).

Approach #1 looks like this:

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:

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.

Any ideas on what I could be doing wrong?

Thanks in advance,

Edit: Link to Selenium users group post.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞花火° 2024-10-17 12:37:04

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

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

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);
}
短叹 2024-10-17 12:37:04

希望这会对您有所帮助:

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

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();
ゞ花落谁相伴 2024-10-17 12:37:04

您必须确保将浏览器插件添加为 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>

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

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

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

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):

  <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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文