How to set https proxy in selenium - selenium

How to set https proxy in selenium and chromedriver? Here is my code
to set up http proxy.
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\lll\\Desktop\\chromedriver.exe");
DesiredCapabilities cap = new DesiredCapabilities();
Proxy proxy=new Proxy();
String proxyIpAndPort= "123.123.123.123:31288";
proxy.setHttpProxy(proxyIpAndPort).setFtpProxy(proxyIpAndPort).setSslProxy(proxyIpAndPort);
cap.setCapability(CapabilityType.ForSeleniumServer.AVOIDING_PROXY, true);
cap.setCapability(CapabilityType.ForSeleniumServer.ONLY_PROXYING_SELENIUM_TRAFFIC, true);
cap.setCapability(CapabilityType.PROXY, proxy);
driver = new ChromeDriver(cap);
but how can i set https proxy? i have tried:
1.String proxyIpAndport = "123.123.123.123:443", it is no internet.
2.String proxyIpAndPort = "123.123.123.123:443#HTTPS", it can't work of proxy.

Related

Selenium Proxy IP Address Configuration IP Not Correct

I have paid/rented a proxy server in brazil
String proxyAddress = "myusername:myuserpass123#196.18.199.51:15464"
proxy.setAutodetect(false);
proxy.setHttpProxy(proxyAddress);
proxy.setSslProxy(proxyAddress);
chromeOptions.setCapability(CapabilityType.PROXY, proxy);
WebDriver webDriver = new ChromeDriver(chromeOptions);
I m running the webdriver on my local computer and I am in Indonesia. When the chrome browser opens up, I can debug and made sure that capabilities were set correctly: I can see the manual proxy setting set to the correct address string above.
However, when webdriver opens https://api.ipify.org/?format=json, it still returns my IP in Indonesia. What am I Missing here? My expectation is because I had configured webdriver to be proxied by a server in Brazil, https://api.ipify.org/?format=json should return Brazilian IP address?
Using Selenium 4 BiDirectional API (https://www.selenium.dev/documentation/webdriver/bidirectional/bidi_api/)
Register Basic Auth.
Some applications make use of browser authentication to secure pages. With Selenium, you can automate the input of basic auth credentials whenever they arise.
//C#
//Console App .NET 6
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
Proxy proxy = new Proxy();
var proxyAddress = "address:port";
proxy.HttpProxy = proxyAddress;
proxy.SslProxy = proxyAddress;
ChromeOptions options = new ChromeOptions();
options.Proxy = proxy;
IWebDriver driver = new ChromeDriver(options);
NetworkAuthenticationHandler handler = new NetworkAuthenticationHandler()
{
UriMatcher = (d) => d.Host.Contains("your-domain.com"), // or set it `true` to enable proxy everywhere
Credentials = new PasswordCredentials("admin", "password")
};
INetwork networkInterceptor = driver.Manage().Network;
networkInterceptor.AddAuthenticationHandler(handler);
await networkInterceptor.StartMonitoring();
driver.Navigate().GoToUrl("https://api.ipify.org/?format=json");
await networkInterceptor.StopMonitoring();

How to Configure the Ports for Communication between Selenium Server and the Browsers

I'm using Selenium Standalone Server 3.141.59 https://www.seleniumhq.org/download
In my code, when a WebDriver is created the Selenium server debugs something like: Starting ChromeDriver on port 28208
Is it possible to configure a range of ports (e.g., 28000-28100) that are allowed to be used by the Selenium server?
Use below code to configure chrome to run on other then default port.
int desiredPortNo = 22300;
ChromeDriverService service = new ChromeDriverService.Builder().usingDriverExecutable(new File("chrome_driver_path")).usingPort(desiredPortNo).build();
WebDriver driver = new ChromeDriver(service);
Update
To use with RemoteWebDriver :
int desiredPortNo = 22300;
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("...", true);
ChromeDriverService service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("driver_path")).usingPort(desiredPortNo)
.build();
service.start();
WebDriver driver = new RemoteWebDriver(service.getUrl(),capabilities);
driver.get("site_url");

Using https proxy with Selenium WebDriver ChromeDriver

I was able to use a proxy successfully, however the proxy is only applied to http, and not https. I am using the code below
Proxy proxy = new Proxy();
proxy.setHttpProxy("myproxy:8080");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--incognito"));
capabilities.setCapability("proxy", proxy);
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
How can I apply the proxy for both http and https?
I found the answer. It's done by the code below
proxy.setSslProxy();
try to use:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("ignore-certificate-errors");
chromeOptions.AddArgument("--ignore-ssl-errors");
switch (ProxyType) // your string variable
{
case "HTTP":
chromeOptions.AddArgument("--proxy-server=http://" + "192.1.1.1:2338"); //example
break;
case "SOCKS":
chromeOptions.AddArgument("--proxy-server=socks5://" + "192.1.1.1:2338");
break;
}

Unable to Fetch Network Traffic Logs via BrowserMob Proxy with Selenium Webdriver

I am not able to Fetch Network Logs using Browsermob Proxy when set Http proxy , it will just create a har file but i am not able to see any logs inside the file
Below is my Code :-
String strFilePath = "./PerformanceLogs/PerformanceLogs.har";
String noProxy = "localhost, 127.0.0.1";
LegacyProxyServer server = new BrowserMobProxyServer();
server.start();
server.setCaptureHeaders(true);
server.setCaptureContent(true);
Proxy proxy = server.seleniumProxy().setHttpProxy(PROXY)
.setFtpProxy(PROXY)
.setSslProxy(PROXY)
.setNoProxy(noProxy);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(capabilities);
server.newHar("NetworkLogs");
driver.navigate().to("https://url-");
driver.findElements(xpath="").click();
pMonitor = new Prism_Selenium_Monitor_Tab(driver);
pMonitor.prism_monitor_credentialDeploy(URL,excelfile[0], excelfile[4]);
Har har = server.getHar();
File harFile = new File(strFilePath);
har.writeTo(harFile);
server.stop();
can anyone please me in this?
Thanks in advance
For some reason you're starting LegacyProxy, try starting BrowserMobProxyServer, i.e.
BrowserMobProxyServer server = new BrowserMobProxyServer();
Default proxy type is HTTP, i.e. try to delete this part:
.setNoProxy(noProxy);
It's actually shouldn't be a problem, but in default case there is no need to name your HAR, i.e. this code is redundant:
server.newHar("NetworkLogs");
you may just:
server.newHar();

Selenium chrome driver socks proxy configuration

I am having troubles in setting socks proxy for chrome driver
Proxy proxy = new Proxy();
proxy.setProxyType(Proxy.ProxyType.MANUAL);
proxy.setAutodetect(false);
proxy.setSocksProxy(ProxyHelper.PROXY_HOST + ":" + ProxyHelper.PROXY_PORT);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.PROXY, proxy);
WebDriver chromeDriver = new ChromeDriver(capabilities);
This configuration gives:
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot parse capability: proxy from unknown error: proxyType is 'manual' but no manual proxy capabilities were found
I think it expects me to fill http, ftp and ssl proxies. But if I fill them; error doesnt raise but my proxy does not work properly too as it tries to use it like http proxy rather than socks proxy.
What can I do?
ChromeOptions options = new ChromeOptions();
options.add_argument("--proxy-server=socks5://" + host + ":" + port);
WebDriver driver = new ChromeDriver(options);
Have you tried using this chromium arg?
--proxy-server="socks5://host:port"
from selenium.webdriver.firefox.options import Options as ff_options
random_proxy = "142.54.61.98:120"
options = ff_options()
firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['proxy'] = {
"proxyType": "MANUAL",
"httpProxy": random_proxy,
"ftpProxy": random_proxy,
"sslProxy": random_proxy
}
profile = webdriver.FirefoxProfile()
profile.set_preference("media.peerconnection.enabled", False)
profile.set_preference("media.navigator.enabled", False)
# profile.set_preference("general.useragent.override", user_agent)
profile.update_preferences()
driver = webdriver.Firefox(capabilities=firefox_capabilities, firefox_profile=profile,
firefox_options=options)