Delete cookies in Selenium Webdriver using IE11 - java

I'm trying to delete all cookies before running a Selenium test.
According to How to clear browser cache automatically in Selenium WebDriver? and
Delete cookies in Webdriver my code is:
Process p = Runtime.getRuntime().exec("RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255");
p.waitFor();
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION,true);
WebDriver webDriver = new InternetExplorerDriver(caps);
webDriver.manage().deleteAllCookies();
testBrowser = new TestBrowser(webDriver, "http://localhost");
testBrowser.manage().deleteAllCookies();
However, the cookies are not deleted.
My environment is: Windows 7, 64bit, IE11.
Any ideas?

A simple solution for your problem is to try deleting the cookies by name. For example, instead of:
webDriver.manage().deleteAllCookies();
Use:
webDriver.manage().deleteCookieNamed("JSESSIONID");

I also encountered the same problem and none of the provided methods for deleting cookies worked.
A possible solution is that you configure your browser to launch with a clean session like this:
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
I found the solution here: http://www.frontendtest.org/blog/ie-cache-selenium/
I hope that will help. At least, it worked for me :)

Related

How to use chrome profile when using browserInDocker with WebDriverManager?

I'm using webdrivermanager with browserInDocker() for my test automation framework. So far it works great. But now I have trouble trying to use a chrome profile.
Usually, when I use selenium webdriver, I simply can set a ChromeOption and it works.
How can I use it with browserInDocker()?
I tried the following code:
public void setupBrowserInDockerWithProfile() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir=/home/seluser/.config/google-chrome/Default");
driver = WebDriverManager
.chromedriver()
.capabilities(options)
.browserInDocker()
.dockerVolumes("/var/lib/docker/volumes/chromeprofile/_data:/home/seluser/.config/google-chrome/Default")
.create();
driver.manage().window().maximize();
driver.get(APP_URL);
}
I tried it with and without a docker volume, but I'm not even sure if I use it right. I'm searching for days now, but could't find a solution yet.
The problem is the argument --user-data-dir=/home/seluser/.config/google-chrome/Default. As explained in the doc, WebDriverManager uses the Aerokube's Docker images. That path is available in the docker-images, but not in Aerokube's. You can drop that argument or change it to the following:
options.addArguments("--user-data-dir=/home/selenium/.config/google-chrome/Default");

How can I set my Firefox browser in private mode in Selenium 2.53.1?

I'm on selenium 2.53.0 (I don't want to update to 3.0 because I need to work on Firefox and Selenium did not implement actions for FF).
My problem is: I try to open FF in private mode (I don't want to keep the cache because I ran a lot of FF instances)
For that, I use a FF profile and I have a certificate, so I accept it.
My code is :
FirefoxDriverManager.getInstance().setup();
capabilities.setBrowserName("firefox");
capabilities.setVersion("46");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffProfile = profile.getProfile("default");
ffProfile.setPreference("browser.private.browsing.autostart", true);
ffProfile.setPreference("browser.privatebrowsing.autostart", true);
//accept the certificate
ffProfile.setAcceptUntrustedCertificates(true);
ffProfile.setAssumeUntrustedCertificateIssuer(false);
capabilities.setCapability(FirefoxDriver.PROFILE, ffProfile);
WebDriver webDriver = new FirefoxDriver();
I tried "browser.private.browsing.autostart" and "browser.privatebrowsing.autostart" because when I did about:config in firefox, i found this two.
I did not received any error, firefox run my test but not in private. Do you have any idea?
I found this post but it haven't have answer.
FirefoxOptions opts = new FirefoxOptions();
opts.addArguments("-private");
FirefoxDrive f = new FirefoxDriver(opts);
working currently with FF v54 and selenium 3.4.0
On your code sample above you are trying twice to set the profile. Try removing one of the arguments? AS long as
ffProfile.setPreference("browser.privatebrowsing.autostart", true);
is not working for you, there is an alternative workaround solution; so you can work until you figure this out.
Find any element at the page you are visiting where you want the private window to be opened. For example:
Driver.FindElement(By.ByXpath(//div[#id="loginButton"])).sendKeys(Keys.chord(Keys.CONTROL, Keys.SHIFT, "P"));
And then send a Ctrl+Shift+p to it..Then you can use the new private window for your test. Perhaps not the best solution but, this will work 100%, just tested on my machine, please comment below if you have trouble getting this right.
Best of luck!

Disable native events on chrome?

Hi I want to disable native events from Chrome driver, I did not find the way to do it, I just found examples for Firefox:
dcBrowser = DesiredCapabilities.firefox();
FirefoxProfile prof2 = new FirefoxProfile();
prof2.setEnableNativeEvents(false);
dcBrowser.setCapability("firefox_profile", prof2);
I tried to do the same for Chrome
dcBrowser = DesiredCapabilities.chrome();
ChromeProfile prof2 = new ChromeProfile();
prof2.setEnableNativeEvents(false);
dcBrowser.setCapability("chrome_profile", prof2);
When I add the import:
import org.openqa.selenium.chrome.ChromeProfile;
I get the following error:
The import org.openqa.selenium.chrome.ChromeProfile cannot be resolved
I know the ChromeProfile class does exists, but since I didn't find any example of disabling chrome native events I don't know if what I did is correct or not or maybe I am missing something, any ideas? Thanks!
There is no equivalent. Chrome always has native events enabled.
See: https://github.com/SeleniumHQ/selenium/wiki/Advanced-User-Interactions#native-events-versus-synthetic-events
If you are using java, you can simply use
super.setNativeEvents(false);

How to open current session Java Selenium?

I have seen many question, but could not implement any answer.
System.setProperty "webdriver.chrome.driver","chromedriverchromedriver.exe");
driver = new ChromeDriver();
// Then what exactly do I do?
//I want to open my regular Chrome window with all plugins and cookies
From http://code.google.com/p/selenium/wiki/ChromeDriver#Starting_Chromium_with_Specific_Flags:
Or to load with a specific profile (note that the default profile
directories can be found here):
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--user-data-dir=/path/to/profile/directory"));
WebDriver driver = new ChromeDriver(capabilities);

Load Chrome Profile using Selenium WebDriver using java

I'm having some troubles getting Selenium loading a chrome profile.
String pathToChrome = "driver/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", pathToChrome);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
String chromeProfile = "C:\\Users\\Tiuz\\AppData\\Local\\Google\\Chrome\\User Data\\Default";
ArrayList<String> switches = new ArrayList<String>();
switches.add("--user-data-dir=" + chromeProfile);
capabilities.setCapability("chrome.switches", switches);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("http://www.google.com");
It starts great and works perfect, but I want to get the default profile loaded because I want to test it with some Extensions enabled and some preferences tested.
Does anybody has an idea why this code isn't working?
I've tested it with Selenium 2.29.1 and 2.28.0 with chromedriver 26.0.1383.0 on windows 7 x64.
This is an old question, but I was still having a problem getting it to work so I did some more research to understand what was happening. The answer from #PrashanthSams is correct, but I was incorrectly adding \Default to the end of the profile path
I found that Chrome appends \Default to the profile path specified in the user-data-dir. So if your profile path is specified as:
user-data-dir=C:\Users\user_name\AppData\Local\Google\Chrome\User Data\Default\
it will append \Default and you will end up at:
C:\Users\user_name\AppData\Local\Google\Chrome\User Data\Default\Default
which is not the same as the profile that you would get if you opened chrome under that user profile normally.
You can also verify your settings if you open a command prompt, navigate to the chrome executable directory, and run chrome with the options specified similar to this:
chrome.exe --user-data-dir="C:\Users\user_name\AppData\Local\Google\Chrome\User Data"
Finally, you can go to a new tab in Chrome and browse to chrome://version/ you will see the actual profile that is being used. It will be listed like:
Profile Path C:\Users\user_name\AppData\Local\Google\Chrome\User Data\Default
These combinations did trick for me :)
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
You should try this
op.addArgument("--user-data-dir=C:/Users/username/AppData/Local/Google/Chrome/User Data/");
op.addArgument("--profile-directory=Profile 2");
Path where Chrome stores the profiles on Linux.
String chromeProfilePath = "/home/(user)/.config/google-chrome/Profile3/";
Creating ChromeOptions object, desabling the extensions and adding the profile I want to use by ".addArguments".
ChromeOptions chromeProfile = new ChromeOptions();
chromeProfile.addArguments("chrome.switches", "--disable-extensions");
chromeProfile.addArguments("user-data-dir=" + chromeProfilePath);
As said above by JasonG, after this point Google-Chrome will append \Default to the String you've provided.
There is a "/Default" folder inside "/Profile3" directory, so what I did was...
I copied the content of "/Profile3" to the "/Default" folder.
Set the Browser System Properties and Path as you usually do, call the constructor that receives a ChromeOption and it will work fine.
WebDriver driver = new ChromeDriver(chromeProfile);
I copied default profile to any other folder and then I do connect to this copy
ChromeOptions options = new ChromeOptions();
options.AddArgument("--user-data-dir=C:\\AnyFolder");
driver = new ChromeDriver(options);
So it uses default profile
I tried in windows and following code works for me:
String userProfile= "C:\\Users\\user_name\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\";
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir="+userProfile);
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com");
How to know whether it is working ?
One way to know is to run the program twice without killing previous instance of the chrome. If the profile is valid, you'll see the second instance "as a new tab" in the first browser window. If it is not working, you get the second instance "as a new browser window".
According to the ChromeDriver wiki, this is a known issue and is currently not possible.
https://code.google.com/p/selenium/wiki/ChromeDriver
None of these above solutions works for me. And after several hours tirelessly research and trying, I already found out this solution
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--start-maximized");
//chrome://version/
chromeOptions.addArguments("--user-data-dir=/home/{username}/.config/google-chrome");
//load default profile
chromeOptions.addArguments("--profile-directory=Default");

Categories

Resources