Using https proxy with Selenium WebDriver ChromeDriver - selenium

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

Related

How to disable dns over https in selenium

I am writing test using selenium, java, chrome.
How can I turn off "dns over https" in chrome settings?
I need it because my intranet DNS have different data than internet ones.
I've tried to add following options.
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.addArguments("ignore-certificate-errors");
options.addArguments("--disable-async-dns");
options.addArguments("--dns-prefetch-disable");
options.addArguments("--disable-web-security");
ChromeDriver driver = new ChromeDriver(options);
It didn't help.
I've even tried to change options by clicking in settings
driver.get("chrome://settings/security");
String disableDNSOverHttpsButton = "/html/body/settings-ui//div[2]/settings-main//settings-basic-page//div[1]/settings-section[4]/settings-privacy-page//settings-animated-pages/settings-subpage/settings-security-page//settings-secure-dns//settings-toggle-button//div/cr-toggle";
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(disableDNSOverHttpsButton)));
driver.findElement(By.xpath(disableDNSOverHttpsButton)).click();
There is response "org.openqa.selenium.NoSuchElementException: "
local_state = {
"dns_over_https.mode": "off",
"dns_over_https.templates": "",
}
options.add_experimental_option("localState", local_state)
The solution is to use setExperimentalOption.
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
Map<String, Object> localState = new HashMap<String, Object>();
localState.put("dns_over_https.mode", "off");
localState.put("dns_over_https.templates", "");
options.setExperimentalOption("localState", localState) ;
ChromeDriver driver = new ChromeDriver(options);

How to setup proxy in QAF

I need to setup proxy in my QAF test method. Below code is working fine without QAF. How to setup the same with QAF implementation?
// #Test
public void sampleTest() {
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("proxy", proxy);
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("--ignore-certificate-errors");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
#SuppressWarnings("deprecation")
ChromeDriver driver = new ChromeDriver(capabilities); // Here opening new window and able
to hit my localhost:8080
//i need to use following QAF methods which also needs to trigger my localhost
get("http://demo.rapidtestpro.com/login.php");
sendKeys("1234567", "//*[#id='accno']");
sendKeys("password123", "//*[#id=\"pass\"]");
System.out.println("Exit in sample test");
}
Also I tried to setup proxy in application.properties file like below
system.http.proxyHost=localhost
system.http.proxyPort=8080
Also tried in Testngconfig.xml file like below
<test name="java Test" enabled="true">
<parameter name="driver.name" value="chromeDriver"/>
<parameter name="system.http.proxyHost" value="localhost"/>
<parameter name="system.http.proxyPort" value="8080"/>
Nothing is triggered in localhost:8080.
As per your working code, you want to set proxy for driver using capabilities. When you are using qaf you can provide driver capabilities in different ways. Simplest way is by setting appropriate property with json value of desired capability. In your case it will look like as below:
driver.name=chromeDriver
chrome.additional.capabilities={"goog:chromeOptions":{"args":["start-maximized","--ignore-certificate-errors"]},"proxy":{"httpProxy":"localhost:8080","sslProxy":"localhost:8080"}}
If you want to set proxy capability for all browsers :
driver.name=<driver name>
#additional capabilities for any driver
driver.additional.capabilities={"proxy":{"httpProxy":"localhost:8080","sslProxy":"localhost:8080"}}
#additional capabilities only for chrome
chrome.additional.capabilities={"goog:chromeOptions":{"args":["start-maximized","--ignore-certificate-errors"]}}
If you don't know what will be the json representation for capability you can print in console and take a reference. For example blow code with print json value of capabilities that you refereed in question:
public static void main(String[] args) {
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("proxy", proxy);
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("--ignore-certificate-errors");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
System.out.println(JSONUtil.toString(capabilities.toJson()));
}
Other way is using driver listener, where you can append capabilities in void beforeInitialize(Capabilities capabilities) For example:
void beforeInitialize(Capabilities capabilities){
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
DesiredCapabilities capabilities = (DesiredCapabilities)capabilities;
capabilities.setCapability("proxy", proxy);
}

How to set https proxy in 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.

How to disable web security using selenium code for IE and Firefox

Written below code for chrome to disable web security
System.setProperty("webdriver.chrome.driver", "D:\\Eclipse\\chromedriver_win32\\chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
/*options.addArguments("test-type");
options.addArgument("--start-maximized");*/
options.addArguments("--disable-web-security");
/* options.addArguments("--allow-running-insecure-content");*/
capabilities.setCapability("chrome.binary","D:/Eclipse/chromedriver_win32/chromedriver.exe");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(options);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(options);
But I want to disable same for FF and IE.

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)