Firefox Profile Not working - firefox

Below is my non-working code for the Firefox profile.
#Before
public void setUp() throws Exception {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.download.dir", "/Location");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/docx");
driver=new FirefoxDriver(profile);
baseUrl = "<URL>";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void test1() throws Exception {
driver.get("URL");
driver.findElement(By.id("Username")).sendKeys("username");
driver.findElement(By.id("Password")).sendKeys("Password");
driver.findElement(By.xpath(".//*[#id='rowCell0']/td[4]/a[4]")).click();// This line of code is for Download Link on the UI
Now once selenium clicks on it, Firefox will again open the confirmation box asking for "Open with" and "Save File".

Please remove
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.showWhenStarting",false);
and try it should work...let me know

I met this issue too and it blocked me several days!
Please make sure the file type you are trying to download, and then configure with the correct value in below line:
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/docx");
Your site source code is sending back a content header saying the file is xxx to match the preferences you have set.
To reference, the types may be: application/octet-stream, application/vnd.ms-excel, text/csv, text/plain, application/zip, application/exe, application/x-zip, application/x-zip-compressed, application/download

Related

Chrome headless browser with corporate proxy authetication not working

I am trying to run a Chrome headless browser sitting behind a corporate proxy. I tried below code. But unable to pass through it.
public class HeadlessChrome
{
WebDriver driver;
#Test
public void createChromeDriverHeadless() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "D:\\LocalData\\workspace\\Drivers and Libraries\\driver\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.setHttpProxy("http://user:pwd#server:port");
proxy.setSslProxy("http://user:pwd#server:port");
// chromeOptions.setCapability("proxy", proxy);
chromeOptions.addArguments("--proxy-server=user:pwd#server:port");
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("start-maximized");
driver = new ChromeDriver(chromeOptions);
driver.get("http://seleniumhq.org");
Thread.sleep(5000);
System.out.println("Title : " + driver.getTitle());
assertTrue(driver.findElement(By.id("q")).isDisplayed());
driver.quit();
}
}
Please help me out.
If you were not using headless you could have used the approach in below link
user:pass proxies with selenium
But with headless extension are currently not allowed. So now your option is add another proxy
chrome -> (intermediate proxy w/o auth) -> corporate proxy w/ auth -> internet
One options is to use polipo
https://www.irif.fr/~jch/software/polipo/
with below config
parentAuthCredentials=username:password
parentProxy=corporateproxy:port
and then use
chromeOptions.addArguments("--proxy-server=http://polipoproxy:port");
The default would be 127.0.0.1:8123 in don't override in polipo config.
Other options you can use
Use squid proxy instead of polipo
Write your own proxy forwarder using python or node or any other language you are comfortable with
There is headless browser called Linken-sphere who cooperates with Luminati. They some good offers . you should check them up.
https://miped.ru/f/threads/linken-sphere-antidetekt-brauzer-novogo-pokolenija.67098/
It's easy to achieve with selenium 4 (currently in beta). You can do it in multiple ways:
You basically need to register a check for whether to apply the credentials for any request for authorization. Works for both - basic and proxy auth popups.
ChromeDriver driver = new ChromeDriver(new ChromeOptions().setHeadless(true));
String USER_NAME = "guest";
String PASSWORD = "guest";
//register our check here
driver.register(UsernameAndPassword.of(USER_NAME, PASSWORD));
driver.get("https://jigsaw.w3.org/HTTP/");
//Click on the link to show an authentication popup
driver.findElement(By.linkText("Basic Authentication test")).click();
String msg = driver.findElement(By.tagName("html")).getText();
assert msg.equalsIgnoreCase("Your browser made it!");
Using CDP Network domain. Doesn't work for proxy authorization popup (for example here is the similar issue in puppeteer which goes down to the chrome project)
ChromeDriver driver = new ChromeDriver(new ChromeOptions().setHeadless(true));
String USER_NAME = "guest";
String PASSWORD = "guest";
DevTools devTools = driver.getDevTools();
//create a cdp session
devTools.createSession();
//enable network first
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
//Open website
driver.get("https://jigsaw.w3.org/HTTP/");
//Create and send the authorization header
Map<String, Object> headers = new HashMap<>();
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(String.format("%s:%s", USER_NAME, PASSWORD).getBytes()));
headers.put("Authorization", basicAuth);
devTools.send(Network.setExtraHTTPHeaders(new Headers(headers)));
//Click on the link to show an authentication popup
driver.findElement(By.linkText("Basic Authentication test")).click();
String msg = driver.findElement(By.tagName("html")).getText();
assert msg.equalsIgnoreCase("Your browser made it!");
Using the CDP Fetch domain. Works for both - basic and proxy auth popups.
ChromeDriver driver = new ChromeDriver(new ChromeOptions().setHeadless(true));
String USER_NAME = "guest";
String PASSWORD = "guest";
DevTools devTools = driver.getDevTools();
//create a cdp session
devTools.createSession();
//enable Fetch first
devTools.send(Fetch.enable(Optional.empty(), Optional.of(true)));
devTools.addListener(Fetch.requestPaused(), requestPaused -> devTools.send(Fetch.continueRequest(requestPaused.getRequestId(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty())));
devTools.addListener(Fetch.authRequired(), authRequired -> devTools.send(Fetch.continueWithAuth(authRequired.getRequestId(), new AuthChallengeResponse(PROVIDECREDENTIALS, Optional.of(USER_NAME), Optional.of(PASSWORD)))));
//Open website
driver.get("https://jigsaw.w3.org/HTTP/");
//Click on the link to show an authentication popup
driver.findElement(By.linkText("Basic Authentication test")).click();
String msg = driver.findElement(By.tagName("html")).getText();
assert msg.equalsIgnoreCase("Your browser made it!");

Facebook authentication shows deprecated method error

I'm using the Facebook C# SDK and trying to authenticate my user. The first part sort of worked, my app showed me the facebook login page inside a browser control.
This is the code I have, I was following this example.
private readonly FacebookClient _fb = new FacebookClient();
private void Browser_Loaded(object sender, RoutedEventArgs e)
{
var loginUrl = GetFacebookLoginUrl();
BrowserControl.Navigate(loginUrl);
}
private Uri GetFacebookLoginUrl()
{
var parameters = new Dictionary<string, object>();
parameters["client_id"] = FacebookSettings.AppID;
parameters["redirect_uri"] = "https://www.facebook.com/connect/login_success.html";
parameters["response_type"] = "token";
parameters["display"] = "page";
if (!string.IsNullOrEmpty(FacebookSettings.ExtendedPermissions))
parameters["scope"] = FacebookSettings.ExtendedPermissions;
return _fb.GetLoginUrl(parameters);
}
After I filled in my details to log into facebook, I got this error:
Any idea's which method is deprecated and how I can fix this?
I used the same code and it's working. And such an error is occuring because of a bug in the facebook API, when display parameter is set to "touch" or "wap". That shouldn't occur when using "page". Try using "popup" as the display.
Try changing the july 2012 Breaking Changes in the app's advanced settings tab(in developer.facebook.com). Refer to this link for information on this issue. And similar issue in facebook developer site.

HttpWebRequest and WebClient returning NotFound on Windows Phone 7 but not i normal console application

I'm trying to download a regular JSON string from this url https://valueboxtest.lb.dk/mobile/categories from a Windows Phone 7 Application.
I have tried to both use WebClient and HttpWebRequest. They both throw an exception
“The remote server returned an error: NotFound”
This is the code for using the WebClient
var webClient = new WebClient();
webClient.DownloadStringCompleted += (client_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri("https://valueboxtest.lb.dk/mobile/categories"));
The eventhandler then just show the content, but e.Result throws the above mentioned exception:
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled) MessageBox.Show(e.Result);
}
For the HttpWebRequest my code looks as follows:
var httpReq = (HttpWebRequest)WebRequest.Create(new Uri("https://valueboxtest.lb.dk/mobile/categories"));
httpReq.BeginGetResponse(HTTPWebRequestCallBack, httpReq);
With the following callback:
private void HTTPWebRequestCallBack(IAsyncResult result)
{
var httpRequest = (HttpWebRequest)result.AsyncState;
var response = httpRequest.EndGetResponse(result);
var stream = response.GetResponseStream();
var reader = new StreamReader(stream);
this.Dispatcher.BeginInvoke(
new delegateUpdate(update),
new Object[] { reader.ReadToEnd() }
);
}
And with the delegate method
delegate void delegateUpdate(string content);
private void update(string content)
{
MessageBox.Show(content);
}
Running it in a console application
Everything works just fine and the JSON string is returned with no problems and I am able to print the result to the console.
Different URL does work on WP7
The weird thing is that the URL http://mobiforge.com/rssfeed actually works fine in both of the above mentioned scenarios.
This issue occurs both in the Emulator and on an actual device.
What could be wrong? Is the REST service returning the data in misbehaving way? I really hope you can help me!
Note: I'm not running Fiddler2 at the same time!
The reason is because that site does not have a valid certificate. Just try it on Mobile Internet Explorer and you'll get the prompt about an issue with the certificate.
How to ignore SSL certificates
Mobile devices are stricter when it comes to SSL certificates.
If you want to get this app into a production environment, you'll either need to write a wrapper for this server (if it's not your own), or get a valid certificate. In the short-term, for testing, you can add a certificate into your device.
Here's a tool which might help you install a certificate.

Testing of the PhpBB forum

I am testing forum PhpBB with help of HTML unit. I want to add message in one of topic of PhpBB forum. I enter the message in textarea and click the submit button, but new page doesn't have my message. What seems to be the problem?
final ArrayList<HtmlForm> formList=(ArrayList<HtmlForm>) page.getByXPath("//form[#id='postform']");
final HtmlTextArea myMessage=formList.get(0).getTextAreaByName("message");
myMessage.type("text");
final HtmlSubmitInput submit_post = formList.get(0).getInputByName("post");
page=submit_post.click();
if (page.asText().contains("text")) {
System.out.println("right");
}
Thank you for help!
One way around this is to construct a custom http post request.
Use FIddler to capture the traffic and then replicate it with HTMLUnit. It should look something like below
public Page postMessage() throws Exception
{
URL url = new URL("YOURURL");
WebRequest requestSettings = new WebRequest(url, HttpMethod.POST);
requestSettings.setAdditionalHeader("Accept", "*/*");
requestSettings.setAdditionalHeader("Content-Type", "application/x-www-form-urlencoded");
requestSettings.setAdditionalHeader("Referer", "REFERER");
requestSettings.setAdditionalHeader("Accept-Language", "en-US,en;q=0.8");
requestSettings.setAdditionalHeader("Accept-Encoding", "gzip,deflate,sdch");
requestSettings
.setRequestBody("YOURPOSTREQUEST");
Page page = webClient.getPage(requestSettings);
return page;
}

Selenium - how to turn on firebug with console, script and net

I have set up a custom firefox profile and load it when selenium RC starts. The profile has firebug installed, and when I manually launch firefox with that profile, firebug is active. However, when selenium launches that profile, firebug is in the lower right, but it is not enabled. How can I ensure it is enabled at launch? OR, how can I enable it (javascript or ?) - I am using the Java API.
If you create a new Firefox profile and assign it to your driver, you need to set the extensions.firebug.allPagesActivation value of the newly created firefox profile to on.
For example in Ruby, with Capybara:
profile = Selenium::WebDriver::Firefox::Profile.new
profile.add_extension("./firebug-1.10.6.xpi")
profile["extensions.firebug.console.enableSites"] = true
profile["extensions.firebug.net.enableSites"] = true
profile["extensions.firebug.script.enableSites"] = true
profile["extensions.firebug.allPagesActivation"] = "on"
Capybara::Selenium::Driver.new app, :browser => :firefox, :profile => profile
See the documentation for Firebug Preferences
package com.mnas.technology.automation.utility;
import java.io.File;
import java.util.logging.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
/**
* #author manoj.kumar
* #email kumarmanoj.mtech#gmail.com
*/
public class AutomationUtility {
static Logger log = Logger.getLogger(AutomationUtility.class.getName());
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
log.info("Starting Automation...");
log.info("Initializing WebDriver...");
FirefoxProfile ffProfile = new FirefoxProfile();
File firebug = new File(getApplicationPath()+"firebug-2.0.7.xpi");
ffProfile.addExtension(firebug);
ffProfile.setPreference("extensions.firebug.currentVersion", "2.0.7"); //(here you can include the version you currently have)
ffProfile.setPreference("extensions.firebug.showStackTrace", true);
ffProfile.setPreference("extensions.firebug.delayLoad", false);
ffProfile.setPreference("extensions.firebug.showFirstRunPage", false);
ffProfile.setPreference("extensions.firebug.allPagesActivation", "on");
ffProfile.setPreference("extensions.firebug.console.enableSites", true);
ffProfile.setPreference("extensions.firebug.defaultPanelName", "console");
WebDriver driver = new FirefoxDriver(ffProfile);
log.info("WebDriver object activated...");
driver.get("http://www.google.com");
String i = driver.getCurrentUrl();
log.info("CurrentURL===>"+i);
//driver.close();
} catch (Exception e) {
}
}
public static String getApplicationPath()
{
String relPath = System.getProperty("relpath");
return (relPath == null ? System.getProperty("user.dir") : System.getProperty("user.home") + relPath) + File.separatorChar;
}
}
The way to do that is to open Firefox using your custom profile. Right-click on the Firebug icon and select "On for All Web Pages". Close Firefox and you should be good to go! That's how I do it.
Here's what works for me in Python:
fp = webdriver.FirefoxProfile()
fp.add_extension(extension='firebug-2.0.xpi')
fp.set_preference("extensions.firebug.currentVersion", "2.0") #Avoid startup screen
fp.set_preference("extensions.firebug.console.enableSites", "true")
fp.set_preference("extensions.firebug.net.enableSites", "true")
fp.set_preference("extensions.firebug.script.enableSites", "true")
fp.set_preference("extensions.firebug.allPagesActivation", "on")
driver = webdriver.Firefox(firefox_profile=fp)
go to the firefox profile location (which is in your java / c# code)
open firefox from that location.
make all your required settings
close and restart firefox browser this time with your webdriver.
that's it, it solves your problem !!

Resources