Selenium 2 WebDriver to use a user profile

I am trying to automate interaction with a site that generates documents using an application like MIME / vnd.wap.xhtml + xml. I am using Selenium 2, WebDriver and FirefoxProfile.

Since Firefox does not handle the aforementioned MIME type, I need to run Firefox with the XHTML mobile profile extension (https://addons.mozilla.org/en-US/firefox/addon/1345/).

After creating the FireFox profile — I named it “selenium” and installed 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 No. 1 is as follows:

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 No. 2 is as follows:

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

Regardless of which piece of code I use, the launching browser instance cannot always process the generated content; The browser asks me for an action to accept the contents of an unrecognized MIME type, as if the extension were not configured correctly.

Any ideas on what I might be doing wrong?

Thanks in advance,

Edit : Link to a message from a Selenium user group .

+4
source share
3 answers

Try starting with an empty profile and add 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); } 
+1
source

Hope this helps you:

 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
source

You must make sure that you add the browser plugin as the DeploymentItem file to the settings file. Some examples (Firebug added in this):

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

Then you need to create a profile that looks something like this:

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

If you add an extension using AddExtension, it must be available inside your selenium driver. Hope this helps.

+1
source

Source: https://habr.com/ru/post/1334286/