Selenium Webdriver get cookies full domain in internet explorer - java

I am trying to obtain the Cookies from the whole domain of a site but I can't seem to get it. Here is my code:
Set<Cookie> cookies = driver.manage().getCookies();
for (Cookie cookie : cookies) {
System.out.println(cookie);
}
This does what I want, however, I have a cookie that has another path value, and this snippet is not returning the correct cookie, any ideas?
Note: This works flawlessly on Chrome and Firefox, the browser in question is IE.

As far as my understanding and knowledge Internet explorer when opened with selenium maintain cookie and cache from earlier activities.
At the time of initiating you can clean the instance.
capabilities = DesiredCapabilities.InternetExplorer();
capabilities.SetCapability("ie.ensureCleanSession", true);
ie.ensureCleanSession
When set to true, this capability clears the cache, cookies, history, and saved form data. When using this capability, be aware that this clears the cache for all running instances of Internet Explorer, including those started manually.
Please do tell me if this information is helpful
Thanks

Related

This browser or app may not be secure selenium/java [duplicate]

I am trying to login to google with selenium and I keep getting the error that "This browser or app may not be secure."
The function I use to log in is:
async function loginToChrome(driver, username, password) {
await driver.get("https://accounts.google.com/signin");
await driver.sleep(1000);
let email_phone = await driver.findElement(
By.xpath("//input[#id='identifierId']")
);
await email_phone.sendKeys(username);
await driver.findElement(By.id("identifierNext")).click();
await driver.sleep(1000);
let passEl = await driver.findElement(By.xpath("//input[#name='password']"));
await passEl.sendKeys(password);
await driver.findElement(By.id("passwordNext")).click();
await driver.sleep(1000);
}
It is the same problem as
https://stackoverflow.com/questions/59433453/unable-to-log-into-google-account-in-selenium-chrome-driver
and
https://stackoverflow.com/questions/59276975/couldnt-sign-you-in-this-browser-or-app-may-be-insecure-python-selenium-chrome
I have tried using both the chrome and firefox web drivers and both don't work.
I have also tried doing .excludeSwitches(['enable-automation']) which also didn't help.
This made me think that maybe the sign-in page could detect that I was running in an automated environment.
I tried this solution that would hide that the app is running in a web driver: Can a website detect when you are using selenium with chromedriver?
I have also looked into the User-Agent to see if that was the problem but what I have found is that it is identical to my regular chrome one.
All of this has not worked which makes leaves me stuck. I have seen solutions that say to use an already created user profile from your normal installation of chrome, but this wouldn't work for my use case.
Has anyone found the solution to this? I have been searching for hours and have come up empty-handed.
EDIT:
It seems like this has been getting a lot of attention recently. I found a solution that allowed me to continue to use an automated client without having too many problems. Switching to Puppeteer.
Look into these packages:
"puppeteer",
"puppeteer-extra",
"puppeteer-extra-plugin-stealth"
EDIT 2:
I have seen this get a lot of attention recently. I found the code that I ended up using to login. I used puppeteer instead of selenium to do this
async function login(
page: Page,
username: string,
password: string,
backup: string
) {
await page.goto("https://accounts.google.com/");
await page.waitForNavigation();
await page.waitForSelector('input[type="email"]');
await page.click('input[type="email"]');
await page.waitForNavigation();
//TODO : change to your email
await page.type('input[type="email"]', username);
await page.waitForSelector("#identifierNext");
await page.click("#identifierNext");
await page.waitFor(1000);
await page.waitForSelector('input[type="password"]');
await page.click('input[type="password"]');
await page.waitFor(500);
//TODO : change to your password
await page.type('input[type="password"]', password);
await page.waitForSelector("#passwordNext");
await page.click("#passwordNext");
await page.waitForNavigation();
}
The followings work me as well:
1. try to login stackoverflow with your google account
2. once login, go to the email
here is solution
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "chromeDriver/chromedriver.exe");
driver = new ChromeDriver();
GeneralClass te = new GeneralClass ();
driver.get("https://accounts.google.com/signin/oauth/identifier?client_id=717762328687-iludtf96g1hinl76e4lc1b9a82g457nn."
+ "apps.googleusercontent.com&as=JS6BM8cjL-8j9votansdkw&destination=https%3A%2F%2Fstackauth"
+ ".com&approval_state=!ChRoYWVvLUlNMk5hSXJWUGlaSVl2WBIfc3lSa0lueENpb29lSU5vbEVpbVNxcUZGaGNkSEJoYw%E2%88%99AJDr988AAAAAXlBKc7PzEomxSzgNqd4wLptVlf0Ny3Qx&oauthgdpr=1&xsrfsig=ChkAeAh8T8JNDxCf2Zah5fb_rQ55OMiF8KmMEg5hcHByb3ZhbF9zdGF0ZRILZGVzdGluYXRpb24SBXNvYWN1Eg9vYXV0aHJpc2t5c2NvcGU&flowName=GeneralOAuthFlow");
te.waitingForElementSendingKey(driver, By.id("identifierId"), "XXXXXXXX#gmail.com");
te.waitingForElementForClickOnly(driver, By.id("identifierNext"));
te.waitingForElementSendingKey(driver,By.name("password"), "PASSSWORD");
te.waitingForElementForClickOnly(driver, By.id("passwordNext"));
Thread.sleep(1500);
driver.get("https://mail.google.com/mail/u/0/#inbox");
Thanks
I just tried something out that worked for me after several hours of trial and error.
Adding args: ['--disable-web-security', '--user-data-dir', '--allow-running-insecure-content' ] to my config resolved the issue.
I realized later that this was not what helped me out as I tried with a different email and it didn't work. After some observations, I figured something else out and this has been tried and tested.
Using automation:
Go to https://stackoverflow.com/users/login
Select Log in with Google Strategy
Enter Google username and password
Login to Stackoverflow
Go to https://gmail.com (or whatever Google app you want to access)
After doing this consistently for like a whole day (about 24 hours), try automating your login directly to gmail (or whatever Google app you want to access) directly... I've had at least two other people do this with success.
PS - You might want to continue with the stackoverflow login until you at least get a captcha request as we all went through that phase as well.
One workaround that worked for me is creating a google account in the chrome instance started by the webdriver. Using this newly created account works for me, but I cannot tell what is exactly the difference between it and other google accounts.
Here's what worked for me:
I am using Puppeteer, but I'd bet it's the same for any automated scripts.
You must have a userDataDirectory so that the browser can use the same storage information.
You must initially run the script with headless: false so that you can get a browser to open. If you try to sign in on the current tab (the tab that was navigated automatically), then you will get that error on every sign-in attempt.
The trick (for me) was to open a new tab, navigate manually, try again.
Next time you run the script, you do not need to login.
Try using undetected_chromedriver library :
!pip install undetected_chromedriver
import undetected_chromedriver as uc
driver = uc.Chrome(executable_path='chromedriver.exe') #change for your path
driver.get('https://accounts.google.com/ServiceLogin')
#continue work code here...
It worked pretty well for me
This error message...
This browser or app may not be secure.
Try using a different browser. If you’re already using a supported browser, you can refresh your screen and try again to sign in.
...implies that the WebDriver was unable to authenticate the Browsing Context i.e. Browser session.
Potential reasons and solution
There can be diverse reason behind this error as follows:
#Raphael Schaad in the article "This browser or app may not be secure" error when trying to sign in with Google on desktop apps mentioned that, if an user can log into the same app just fine with other Google accounts, then the problem must lie with the particular account. The possible reason, it is the only account where user is using Two Factor Authentification.
Another pottential reason can be usage of Less secure apps. If an app or site doesn’t meet google-chrome's security standards, Google may block anyone who’s trying to sign in to your account from it. Less secure apps can make it easier for hackers to get in to your account, so blocking sign-ins from these apps helps keep your account safe.
Solution
In these cases the respective solution would be to:
Disable Two Factor Authentification for this Google account and execute your #Test.
Allow less secure apps
You can find a detailed discussion in Sign in to gmail account fails (selenium automation)
tl; dr
A couple of relevent documentation:
Sign in with a supported browser

Log in to facebook using selenium in Java

I try to log in to facebook using selenium library but I occur some problems. After it inputs the credentials and click the login button I get an error which says:
Cookies Required
Cookies are not enabled on your browser. Please enable cookies in your browser preferences to continue.
I've looked over the internet for an answer so I've tried with driver.manage().getCookies(); and I get the same result.
Here's my code:
System.setProperty("webdriver.chrome.driver", DRIVERLOCATION);
WebDriver driver = new ChromeDriver();
driver.manage().getCookies();
driver.get("https://www.facebook.com");
driver.findElement(By.id("email")).sendKeys(USERNAME);
driver.findElement(By.id("pass")).sendKeys(PASSWORD);
driver.findElement(By.xpath("//input[starts-with(#id, 'u_0_')][#value='Log In']")).click();
I know in the question I said that I try to reach www.facebook.com but in the real project I was reaching a profile.
It works just fine when you go to https://www.facebook.com but the error occurs when your going to a profile or if you have second authentication activated.

Selenium Basic Authentication via URL HeadlessChrome ( On Linux Server)

In my Selenium Tests I need to test a webpage where I use a basic Authen,
Knowing that I am using Chrome Headless Java and Selenium WebDriver.
On my machine 'locally' It works perfectly using driver.get("https://admin:admin#localhost..");
and then
driver.get("https://localhost..") for example.
I know that Chrome doesn't support this function anymore but I managed to make it work based on someone's solution here by passing the first URL with credentials and the second without.
But when I run it on remote (Jenkins) On Linux servers I get the following Error
the configuration of your browser does not accept cookies
. I don't have vision on the servers when I can configure Chrome ..any ideas how to make it work without facing that problem.
I know a lot of people asked that question before, But I didn't find any valide answer for my issue.
try ChromeDriver 2.45 (changelog) or change the location, where it is supposed to save the cookies:
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/your/custom/profile");
otherwise (per default) it would create a new temporary directory each time it starts a session.
ChromeOptions options = new ChromeOptions();
//Command line flag for enabling account consistency. The default mode is disabled.
options.addArguments("--account-consistency");
//Chrome that will start logging to a file from startup
options.addArguments("--log-net-log=C:/some_path/resource/log.json");
//Sets the granularity of events to capture in the network log.
options.addArguments("--net-log-capture-mode=IncludeCookiesAndCredentials");
Try this, basically, it saves the logs on startup of chrome browser, then it will set the account consistency. Anywhere from the log, you can debug the issue.
Hello I managed to fix the problem (I forgot to mention that our website is protected by Siteminder) so I did the following following:
1-We inject the USER and the PASSWORD on the URL :
The issue we faced was that the displayed prompt wasn’t part of the page’s HTML and it was hard for us to catch it using Selenium. We managed this by directly injecting the user login and the user password in the URL as follow :
‘https://USERNAME:PASSWORD#basicAuthentURL’
This will launch the Chrome session. Beware, this is only the first step of the process. The user identification have not been performed yet.
2- We create a new cookie :
After launching the URL, we have to manually create a cookie called « SMCHALLENGE » and add it to current session with Selenium, for example in JAVA :
new Cookie("SMCHALLENGE", "YES");
3- Call the URL without the user credencials :
As the SMCHALLENGE cookie is now set, the last step is to call the URL again (https://basicAuthentURL ). The SMCHALLENGE cookie will be deleted once the authentication succeed and a SMSESSION cookie will be generated by Siteminder.
The SMSESSION cookie now allows us to call the application and sucessfully pass Siteminder as if normally logged in (via SSO).
Let me know if you try this out.

Application throws warning message to enable cookies while running through IEDriver with selenium webdriver

I was automating an angularjs application using selenium webdriver. I am using IEDriver 32 bit version to driver the IE execution. Typically, the scenario is admin user will register a user, this user will click on the registration link mailed to their maid id. Then it will input some required information, automatically it will be navigated to login page where user have to input the username and password and login.
But application warns to enable cookies at this point of time. If we are renavigating to the login page there is no issue.
This issue is not reproducible everytime.
Is there any way to enable cookies while setting up the IE profile?
There are many ways as you can find cookies in the below path
In Windows 8 for IE10, cookies are saved in the hidden protected OS folders below:
C:\Users(user name)\AppData\Roaming\Microsoft\Windows\Cookies
you can open IE and then navigate to internet options/privacy/advanced privacy settings.
There you can accept and block cookies.

Unable to clear Browser cookies and Session Data with Selenium

I have been using Java Selenium WebDriver along with Appium to perform tests on Mobile environment be it Emulator(Genymotion) or Physical devices (Android). I am using chromedriver, which I am using to perform tests on Web App in Chrome browser. I am looping my cases for multiple sets of data but the application requires a full browser Cookie and all Session data to be deleted before each loop starts.
I tried using driver.Manage().Deleteallcookies(), but it did not work out for me. I read in some threads to try creating a new session of the browser before each loop. So I tried driver.quit() but it ends the chromedriver session and ends the test. I also tried driver.close() but got the same results as driver.quit().
Can any one suggest a way to delete the browser cookies and session data in chrome browser??
My Appium version:1.3.4.1
Chromedriver version:2.3
Device/Emulator i am trying to test on : Nexus5/Samsung Note 3 Android:4.4.4/5.0
You can try using the following to ensure a clear session. Note I never tested that myself. My understanding is that selenium by default create a new session unless you specified something different or load a profile.
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
ChromeDriver driver = new ChromeDriver(capabilities);

Categories

Resources