Log in to facebook using selenium in Java - 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.

Related

Unable to login to Google service with Selenium in Java: “This browser or app may not be secure.”

I have this problem with selenium, i try to run my code below but i have this error: https://i.postimg.cc/VNd3F4rm/u2zrn.jpg .
Image of my "Signing in to Google": https://i.stack.imgur.com/w3POX.png (As you can see "App password" does not appear to me)
I have already tried to disable the "less secure apps" section in account settings and checked if JavaScipt was actived, but without success.
WebDriver driver= new ChromeDriver();
driver.navigate().to("https://accounts.google.com/signin");
driver.findElement(By.name("identifier")).sendKeys("email#gmail.com");
driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[2]/div/div[2]/div/div/div[2]/div/div[2]/div/div[2]/div/div/button")).click();
driver.findElement(By.name("password")).sendKeys("*******");
I believe you need to go to your Google Account settings and under Security, you need to register your test app with a password. I had to do this in order to implement in Cucumber a way to send and read emails.
Then, from your test application, you will use those credentials (not your real Google creds) to authenticate and do what you need. You could also try this.
I have another solution in python that might help you.
Use Seleniumwire with undetected browser v2
Note: put chromedriver in your sys path.
from seleniumwire.undetected_chromedriver.v2 import Chrome, ChromeOptions
import time
options = {}
chrome_options = ChromeOptions()
chrome_options.add_argument('--user-data-dir=hash')
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--disable-dev-shm-usage")
# chrome_options.add_argument("--headless")
browser = Chrome(seleniumwire_options=options, options=chrome_options)
browser.get('https://gmail.com')
browser.find_element_by_xpath('//*[#id="identifierId"]').send_keys('your-email')
browser.find_element_by_xpath('//*[#id="identifierNext"]/div/button').click()
time.sleep(5)
browser.find_element_by_xpath('//*[#id="password"]/div[1]/div/div[1]/input').send_keys('you-password')
browser.find_element_by_xpath('//*[#id="passwordNext"]/div/button').click()
In addition to this, selenium wire has many awesome features, check out Github repository

how to handle Mobile OTP using selenium

There is one case that, when i open the URL manually in chrome browser and give the credentials.
After clicking on sign in button one OTP will come to mobile. There is one checkbox after entering the OTP. Don't ask OTP for this browser. If we click on that check box and login into the portal. Then from next time if we open same browser and access the portal, it will not ask for OTP. So can we automatically open same browser every time after closing browser. Without calling new chrome instance.
You can use the default profile to use the local chrome profile once the OTP is completed manually.
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/your/custom/profile");
ChromeDriver driver = new ChromeDriver(options);
You can get the default profile path as shown in the below screenshot.
Please open chrome://version in the browser to see what profile Chrome is using.

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.

Firefox selenium webdriver gives "Insecure Connection"

I've created a Maven project with 20 tests made with Selenium Webdriver (java). Now, when I want to execute my Maven project, I get sometimes the following error:
Mozilla error
This is due to login every test. So, when I want to run 20 tests, sometimes that error appears and I can't continue my test, so it returns "Failed test" in Selenium Webdriver.
Does anybody know how to fix this problem?
I've tried to put "Thread.sleep(30000);" at the end of every test to give them some time "not to seem a robot", but it doesn't work...
Thanks so much for your help guys!
Here is the Answer to your Question:
The Real Issue:
The URL/Connection with which you are a working if it is Not Secure then whenever you access the URL through Mozilla Firefox 53.0, Firefox will display a lock icon with red strike-through red strikethrough icon in the address bar. Now when URL gets loaded the cursor by default will be positioned on Username field and there will be popup showing a message This connection is not secure. Logins entered here could be compromised. Learn More like this:
Now your script through Selenium enters the username within the Username input field and the Not Secure popup overlays the Password input field.
Next if you try to call the click() or sendKeys() operation in the Password input field the Not Secure popup receives the click and Insecure password warning in Firefox page opens up in the next tab along with Selenium shifting its focus to new tab. Hence test-case starts Failing.
Solution:
In these cases the best solution is:
Create a new Mozilla Firefox Profile. You will find the documentation here. For Example, I have created a Firefox Profile by the name debanjan
Configure the Firefox Profile debanjan to ignore all the UntrustedCertificate issues.
Rerun your Test Script without any issues.
Here is a sample code block to disable insecure_field_warning:
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("debanjan");
testprofile.setAcceptUntrustedCertificates(true);
testprofile.setAssumeUntrustedCertificateIssuer(true);
testprofile.setPreference("security.insecure_field_warning.contextual.enabled", false);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, testprofile);
dc.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(dc);
driver.manage().window().maximize();
driver.navigate().to("http://demosite.center/wordpress/wp-login.php");
Let me know if this Answers your Question.

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.

Categories

Resources