How to Setup Private Proxy with Selenium? - java

I've been trying for days to setup a private proxy (with authentication) in Selenium using Firefox. However, no matter what I do I'v been unsuccessful.
Currently, I have tried the following two approaches and in both cases Firefox launches normally without any proxy.
Proxy proxy = new Proxy();
proxy.setHttpProxy(proxyHost + proxyPort);
proxy.setSocksUsername(proxyUsername);
proxy.setSocksPassword(proxyPass);
DesiredCapabilities cap = DesiredCapabilities.firefox();
cap.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(cap);
driver.get("http://google.com");
I have also tried the following:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", proxyHost);
profile.setPreference("network.proxy.http_port", proxyPort);
profile.setPreference("network.proxy.http", "user:pass#1.1.1.1");
profile.setPreference("network.proxy.http_port", proxyPort);
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://google.com");
How can I setup http private proxies (with user name and password in Selenium with Firefox)?
I am using Java.
Thanks

const {Builder, By, Key, until} = require('selenium-webdriver');
const proxy = require('selenium-webdriver/proxy');
(async function example(){
let driver = await new Builder().forBrowser('firefox').setProxy(proxy.manual({
http: 'zproxy.lum-superproxy.io:22225',
https: 'zproxy.lum-superproxy.io:22225'
})).build()
try {
await driver.get('http://lumtest.com/myip.json');
driver.switchTo().alert()
.sendKeys('lum-customer-USERNAME-zone-YOURZONE'+Key.TAB+'PASSWORD');
driver.switchTo().alert().accept();
} finally {
await driver.quit();
}
})();
Here is my sample code when I use Luminati proxy for Selenium.

You can try using browsermob proxy.
Here you go for example

Find solution:
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': PROXY_HOST,
'socksUsername': 'name',
'socksPassword': 'pass'
})
driver = webdriver.Firefox(proxy=proxy)

Related

Proxy is not working in chromeOptions in webDriver with selenium in Java

I'm using proxy in chrome driver but is it not working it is showing my local network only instead of proxy network.
Proxy proxy = new Proxy();
proxy.setProxyType(Proxy.ProxyType.MANUAL);
proxy.setHttpProxy("proxyhost:proxyport");
proxy.setSocksUsername("ProxyUsername");
proxy.setSocksPassword("ProxyPassword";
chromeOptions.setCapability("proxy", proxy);
WebDriver driver = new ChromeDriver(chromeOptions);
Try below code:
Proxy proxy = new Proxy();
proxy.setAutodetect(false);
proxy.setProxyType(Proxy.ProxyType.MANUAL);
proxy.setHttpProxy("proxyhost:proxyport");
proxy.setSocksUsername("ProxyUsername");
proxy.setSocksPassword("ProxyPassword";
chromeOptions.setCapability("proxy", proxy);
WebDriver driver = new ChromeDriver(chromeOptions);
Another way of doing same, Please try this
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--proxy-server=http://user:password#proxy.com:8080"));
WebDriver driver = new ChromeDriver(capabilities);

Selenium: changing proxy in Firefox

i'm writing tests in selenium and want to change proxy to auto-detect in firefox, default is proxy from system settings. How to do it?
I have code below:
public class SodirRejestracja {
String baseUrl = "http://google.pl";
String driverPath= "C:\\geckodriver.exe";
WebDriver driver;
#BeforeTest
public void beforeTest() {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 2);
System.setProperty("webdriver.gecko.driver", driverPath);
driver=new FirefoxDriver(profile);
}
#Test
public void test(){
driver.get("http://google.com");
}
}
Code above is from How do I set a proxy for firefox using Selenium webdriver with Java?
but in line driver=new FirefoxDriver(profile) i get: "The constructor FirefoxDriver(FirefoxProfile) is undefined"
A sample (not tested but that compiles) that should do it
String proxyName = <yourProxyHost> + ":" + <yourProxyPort>;
Proxy proxy = new Proxy();
proxy.setHttpProxy(proxyName)
.setFtpProxy(proxyName)
.setSslProxy(proxyName);
DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);
FirefoxOptions options = new FirefoxOptions(desiredCapabilities);
driver = new FirefoxDriver(options);
This code works
Proxy proxy = new Proxy();
proxy.setProxyType(Proxy.ProxyType.AUTODETECT);
FirefoxOptions options = new FirefoxOptions();
options.setProxy(proxy);
driver = new FirefoxDriver(options);

How to set proxy for Chrome browser in selenium using Java code

I am trying to run my selenium java code to test a webpage. But webpage is not loading because of network restrictions. When I set the proxy manually and hit the url in browser it works fine. Now I need to pass those proxy setting while running the selenium code. Please help me on this.
I tried below code, but still it shows the same error:
Proxy p=new Proxy();
// Set HTTP Port to 7777
p.setHttpProxy("www.abc.com:8080");
// Create desired Capability object
DesiredCapabilities cap=new DesiredCapabilities();
// Pass proxy object p
cap.setCapability(CapabilityType.PROXY, p);
// Open firefox browser
WebDriver driver=new ChromeDriver(cap);
Passing a Capabilities object to the ChromeDriver() constructor is deprecated. One way to use a proxy is this:
String proxy = "127.0.0.1:5000";
ChromeOptions options = new ChromeOptions().addArguments("--proxy-server=http://" + proxy);
WebDriver webDriver = new ChromeDriver(options);
Issue is resolved with below code -
Proxy proxy = new Proxy();
proxy.setHttpProxy("yoururl:portno");
proxy.setSslProxy("yoururl:portno");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("proxy", proxy);
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
Passing a Capabilities object to the ChromeDriver() constructor is deprecated. You can find new official doc here.
ChromeOptions chromeOptions = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.setAutodetect(false);
proxy.setHttpProxy("http_proxy-url:port");
proxy.setSslProxy("https_proxy-url:port");
proxy.setNoProxy("no_proxy-var");
chromeOptions.setCapability("proxy", proxy);
driver = new ChromeDriver(chromeOptions);
DesiredCapabilities dc;
dc = DesiredCapabilities.chrome();
System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "9090");
System.setProperty("https.proxyHost", "127.0.0.1");
System.setProperty("https.proxyPort", "9090");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("--disable-extensions");
dc.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(dc);
Another way of doing it:
boolean useProxy = true;
ChromeOptions options = new ChromeOptions().addArguments(
'--headless',
'--no-sandbox',
'--disable-extensions',
'--proxy-bypass-list=localhost');
if (useProxy) {
options.addArguments("--proxy-server=http://ProxyHost:8080");
}
WebDriver driver = new ChromeDriver(options);
See https://peter.sh/experiments/chromium-command-line-switches/ for more Chrome switches
Another way to set a proxy:
Proxy proxy = new Proxy();
proxy.setHttpProxy("<HOST:PORT>");
proxy.setSslProxy("<HOST:PORT>");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setCapability("proxy", proxy);
WebDriver webDriver = new ChromeDriver(chromeOptions);

How can I set the browser version in Selenium RemoteWebDriver?

When I use HtmlUnitDriver I can set my own browserVersion like:
private HtmlUnitDriver initDriver() {
BrowserVersion browserVersion = new BrowserVersion(
BROWSER_NAME,
BROWSER_OS,
USER_AGENT,
Float.parseFloat(BROWSER_VERSION));
browserVersion.setBrowserLanguage(BROWSER_LANGUAGE);
browserVersion.setHtmlAcceptHeader(HTML_ACCEPT_HEADER);
return new HtmlUnitDriver(browserVersion);
}
Is it possible to do the same with the RemoteWebDriver ?
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:4444/wd/hub"),
myCapabilities);
In Capabilities I can set myCapabilities.setBrowserName("htmlunit"). Is that all that I can do ?
EDIT:
To be clear, I need 3 things:
a) Selenium-server-standalone to be able to reuse the same old SessionID
b) To make browser that works only from console (so no firefox afaik)
c) To make http requests the same as the latest browsers so there will be no difference in the server logs.
You can also set a lot of parameters, for example:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("firefox");
capabilities.setVersion("35.0");
capabilities.setPlatform(Platform.VISTA);
try {
driver = new RemoteWebDriver(new URL("http://192.168.63.109:5555/wd/hub"), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
A bit more settings to skip dialog on file downloading using custom FirefoxProfile:
public static WebDriver setDriver() {
FirefoxProfile fxProfile = new FirefoxProfile();
fxProfile.setPreference("browser.download.folderList", 2);
fxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
fxProfile.setPreference("browser.download.manager.showWhenStarting", false);
fxProfile.setPreference("browser.download.dir", dir);
fxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-excel," +
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
return new FirefoxDriver(fxProfile);
}

How to set Proxy setting for Chrome in Selenium Java

I am able to set proxy settings for Firefox as below.
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setProxyType(ProxyType.MANUAL);
proxy.setHttpProxy(CONFIG.getProperty("hostname"));
proxy.setSslProxy(CONFIG.getProperty("hostname"));
proxy.setFtpProxy(CONFIG.getProperty("hostname"));
proxy.setSocksUsername(CONFIG.getProperty("username"));
proxy.setSocksPassword(CONFIG.getProperty("password"));
FirefoxProfile fp = new FirefoxProfile();
fp.setProxyPreferences(proxy);
driver = new FirefoxDriver(fp);
builder = new Actions(driver);
bckdbrowser = new WebDriverBackedSelenium(driver, ConfigReader.ENVIRONMENT_URL);
But I need to setup for Chrome as well.. Can any one assist me how to do ?
Thanks
Raj
You can try using the DesiredCapabilities class, like this:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--proxy-server=http://user:password#proxy.com:8080"));
WebDriver driver = new ChromeDriver(capabilities);
Try this code:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", ProxyType.AUTODETECT.ordinal());
WebDriver driver = new FirefoxDriver(profile);
here we have one more solution....it's worked for me

Categories

Resources