for the past two days I've been trying to use a proxy with Selenium, that's not exactly the issue though. The issue is that the proxy is private meaning it needs authentication to use it (Username and Password) but I can't figure out how to do it.
I'm using a Firefox driver, with a profile like so:
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.SetPreference("network.proxy.type", 1);
firefoxProfile.SetPreference("network.proxy.http", "23.95.115.87");
firefoxProfile.SetPreference("network.proxy.http_port", 80);
var driver = new FirefoxDriver(firefoxProfile);
driver.Navigate().GoToUrl("http://ipchicken.com");
I figured that it would ask me for the username and password (in a dialog box) yet nothing happens, it just navigates to the webpage, and displays my own IP. I can't find anything really on this, any help guys? Thank you so much.
I am not an expert in Selenium but I can help you in making your proxy authentication free.
If you are on Windows, download something like CC-Proxy ( Its free for a single user) and add your proxy as a cascading proxy. This will create a local proxy server on your computer which won't require username/password. Then you can use the local proxy server in selenium.
If you are on Linux, you can use wine to run CC-Proxy or you can use tinyproxy or squid ( it is an overkill).
Comment if you face problem in setting up CC-Proxy or tinyproxy.
Related
im writing a automation tool in visual studio with c#.
im using selenium with chromedriver. i need proxy user pass auth for my session. But i try so many times and so many sources but didnt work.
How to basicly user pass ip port proxy for my chromedriver session. Thanks for help.
There are a couple of options.
Configuring a proxy using ChromeOptions and org.openqa.selenium.Proxy
If you want the solution to be easily portable to run on other systems, you can try configuring a proxy using ChromeOptions and org.openqa.selenium.Proxy. There will be some additional methods in here to configure your proxy, but we need to know what type of proxy and type of authentication is supported.
// Add the WebDriver proxy capability.
Proxy proxy = new Proxy();
proxy.setHttpProxy("myhttpproxy:3337");
options.setCapability("proxy", proxy);
// Add a ChromeDriver-specific capability.
ChromeDriver driver = new ChromeDriver(options);
Create a new chrome profile with proxy configuration
Alternatively, you could create a profile for chrome with the proxy configured and tell chrome driver to work with that. This would be difficult, if not impossible, to port to other systems without manual setup, unless you containerized it, but that's out of scope here.
As of chrome 71, head to chrome settings at chrome://settings/ and choose Manage other people. Create a new person to represent your proxy profile. Configure the proxy using the browser the way you normally would for manually connecting.
Locate the path to your profile, something like: C:\Users\<username>\AppData\Local\Google\Chrome\User Data\Profile 1 if you're on windows. You can easily find by navigating to chrome://version/ and looking for Profile Path
Utilize ChromeOptions to pass the option for setting the user data directory for chrome on startup
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/your/custom/profile");
Some snippets pulled from documentation for chromedriver
I want to block c# controlled Google ChromeDriver via Selenium
Is that possible?
This is how i start the selenium and navigate
var Driver = new ChromeDriver();
Driver.Navigate().GoToUrl(URL)
Lets say i want that particular Chrome instance to do not connect http://googleads.g.doubleclick.net:443
Is this possible?
When that chrome instance requests to connect http://googleads.g.doubleclick.net:443, it should be automatically blocked or maybe return 127.0.0.1 I do not know
I need some solution which will prevent driver waiting response
Ok after i saw the answer i think i couldnt clarify the question well enough
The thing i want is, i am navigating to a page, which makes a lot of unnecessary requests to other hosts. I want to block those hosts, so page load timings would be much better
I tried via editing hosts file in etc and now it is 10 times better
But this is system wide
I wonder same may apply to chromedriver or not
The WebDriver does not allow this level of control (on purpose), so the short answer is no, you can't do that through it.
There is a solution though - just proxy all the traffic, and blacklist the offending URIs; or whitelist the one you're working with. In any ways, make the proxy return a reasonable response in time (a pixel file, or 404) for any unneeded URIs.
Sample code (my C#is rusty :):
ChromeOptions options = new ChromeOptions();
options.AddArguments("--proxy-server=XXX.XXX.XXX.XXX");
var Driver = new ChromeDriver(options);
(for Firefox it's a bit different, it's set as capabilities there)
There are a lot of options for the proxy, from the squid legend, the tinyproxy I've used in the past for similar purposes, to whatever will suit your environment and needs.
I'm currently developing a tool that uses remote desktop sessions using the RDP protocol. I'm able to connect and disconnect to a remote desktop but I want to add the logoff functionality to it. I've looked far and wide in the various documentation but have not been successful yet.
I'm currently using AxMsRdpClient6NotSafeForScripting because I'm not yet able to get it working with the regular AxMsRdpClient6 without the NotSafeForScripting (or with any other version for that matter). So the issue is probably that this doesn't support logoff and I have to use the AxMsRdpClient6 version, I suppose ...
Any help or suggestions would be greatly appreciated.
A demo of the code I'm currently using can be found below
AxMsRdpClient6NotSafeForScripting rdp = new AxMsRdpClient6NotSafeForScripting();
host.Child = rdp;
rdp.CreateControl();
rdp.Server = serverName;
rdp.UserName = username;
IMsTscNonScriptable secured = (IMsTscNonScriptable)rdp.GetOcx();
IMsRdpClient10 client = (IMsRdpClient10)rdp.GetOcx();
client.DesktopHeight = 750;
client.DesktopWidth = 750;
secured.ClearTextPassword = pwd;
rdp.Connect();
This is independent of the ActiveX control version you use actually - the control doesn't have a logoff method, because it isn't an action exposed by the RDP protocol (at least as far as I remember).
It is possible to remotely log off your RDP session, but you have to use the Win32 WTSLogoffSession function (https://msdn.microsoft.com/en-us/library/aa383836(v=vs.85).aspx) to do it. Unfortunately that's a little painful to do from C# since you'd need to deal with PInvoke and marshalling structures in order to find the right session, but it should at least be possible.
I'm trying to use Selenium WebDriver to automatically login in to a site with a user-name and password. I've done my research and I don't believe this feature is supported by WebDriver, so I need to find another way. The site I'm trying to automate logging into is located here.
When prompted to login a popup window comes up that doesn't seem to be part of the browser. I'm using Firefox and Chrome. It seems Windows API may be required? I already tried passing the credentials in the URL but that didn't work. Also tried sendkeys, but received a Windows exception that the application was not accepting Windows messages. I also tried switching the current handle using driver.windowhandles but the popup doesn't seem to be a new handle.
Does anybody have any ideas? I'm kinda stuck. The preliminary code to get to the popup window is:
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.portal.adp.com");
string currentWindow = driver.CurrentWindowHandle;
IWebElement userLogin = driver.FindElement(By.Id("employee"));
userLogin.Click();
The popup you are seeing is prompted by web server and is a authentication prompt. Selenium doesn't support this operation.
One of the way to handle this limitation is to pass user and password in the url like like below:
http://user:password#example.com
More info available here : http://aleetesting.blogspot.in/2011/10/selenium-webdriver-tips.html
I wanted my answer out there because I think I've solved it. This answer does not require passing the credentials through the URL (for those of you that are unable to like me). It also does not require any custom Firefox Profiles or extensions to be installed or included with the solution or installed onto the browser eliminating cross-machine compatibility issues.
The issue with me was that the authentication could not be completed via passing the credentials through the URL because the login was behind a proxy.
So, I turned to windows automation toolkits and found AutoIT. Using AutoIT and Selenium, you can login automatically by sending the username and password to the windows dialog that appears. Here's how (note the steps below are for c#:
1 - Download AutoIT from http://www.autoitscript.com/site/autoit/downloads/
2 - Add the autoit .dll to your project references.
Right click on references, select Add Reference. Next click the browse button and browse to the dll location (most default installations it will be c:\Program Files (x86)\AutoIt3\AutoItX\AutoItX3.dll), and add to project.
3 - use AutoIT and Selenium like this (assuming your web driver is already initialized):
//Initialize AutoIT
var AutoIT = new AutoItX3();
//Set Selenium page load timeout to 2 seconds so it doesn't wait forever
Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(2));
//Ingore the error
try
{
Driver.Url = url;
}
catch
{
return;
}
//Wait for the authentication window to appear, then send username and password
AutoIT.WinWait("Authentication Required");
AutoIT.WinActivate("Authentication Required");
AutoIT.Send("username");
AutoIT.Send("{TAB}");
AutoIt.Send("password");
AutoIT.Send("{ENTER}");
//Return Selenium page timeout to infinity again
Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(-1));
Anyway, that's it, and it works like a charm :)
Also note that there are some special characters that need to be escaped in AutoIT using the sequence "{x}". For example, if your password is "!tRocks", you'd need to pass it into AutoIT as "{!}tRocks".
Happy automating.
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("network.http.phishy-userpass-length", 255);
profile.SetPreference("network.automatic-ntlm-auth.trusted-uris", hostname);
Driver = new FirefoxDriver(profile);
hostname is your URL (example.com) then try to
Driver.Navigate().GoToUrl(http://user:password#example.com);
I just got done working on a prototype project that is supposed to handle exactly this kind of situation.
It utilizes BrowserMob, a popular open source proxy, to perform the authentication.
SeleniumBasicAuthWrapper Hope it helps! It is still a work in progress, but hopefully we'll get any kinks or defects ironed out in the near future.
I have a small C# solution used to check users credentials. It works fine for two of my teammates, but on my PC I get an exception.
The relevant code:
PrincipalContext context = new PrincipalContext(ContextType.Domain);
if (context.ValidateCredentials(System.Environment.UserDomainName + "\\" + usr, pwd))
return true;
else
return false;
And the exception is:
DirectoryOperationException, "The server cannot handle directory requests.".
I tried creating context with the explicit server name and the 636 port number, but this didn't help as well.
Any ideas?
I had this problem too using IIS Express and VS 2010. What fixed it for me was a comment on another thread.
Validate a username and password against Active Directory?
but i'll save you the click and search... :) Just add ContextOpations.Negotiate to you Validate Credentials call like below.
bool valid = context.ValidateCredentials(user, pass, ***ContextOptions.Negotiate***);
I had this issue: things were working on my dev machine but didn't work on the server. Turned out that IIS on the server was set up to run as LocalMachine. I changed it to NetworkService (the default) and things started working.
So basically check the user of the app pool if this is running on IIS.
I had to just create a new app pool and assign it .NET 2.0, then assign the new app pool to our web app, and it started working. We had .NET 3.5 SP2, so the hotfix wasn't ideal for us. Since the WWW service is usually Local System, I questioned that too. But since it was .NET and security related, I gave a shot at the app pool first and it worked.
Perhaps you need the hotfix?
FIX: DirectoryOperationException exception
And you are an Admin or the id that your service is running under is an Admin on your PC right?
I take it you already looked into this:
System.DirectoryServices.Protocols
"You may receive a less than helpful DirectoryOperationException(“The server cannot handle directory requests.”) what isn’t quite so amusing about this is that it didn’t even try to communicate with the server. The solution was to add the port number to the server. So instead of passing “Server” to open the LdapConnection, I passed “server:636”. By the way, LDAPS is port 636 – rather than the 389 port used by LDAP."
Good point, I wouldn't expect that Win7/.NET 3.5 would need that patch. How about the info provided in this question:
Setting user's password via System.DirectoryServices.Protocols in AD 2008 R2