How to get this Selenium script to login without being detected? - selenium

Before this gets taken down as a duplicate, I have already tried all of the options available here: Is there a version of Selenium WebDriver that is not detectable?.
I am trying to create a Selenium bot in Python which can successfully login without getting blocked due to bot detection. My current script is below and I have created a test account for anyone to access and help me get this working. The correct answer here would ideally be a working version of this script that I can use.
Username: TestAccount
Password: StackOverflowPass1
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
##### open depop 'likes' page #####
url = "https://www.depop.com/login/"
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(url)
driver.implicitly_wait(2)
driver.get(url)
time.sleep(2)
accept_cookies = driver.find_element_by_xpath('/html/body/div[1]/div/div[3]/div[2]/button[2]')
accept_cookies.click()
time.sleep(2)
username = driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/form/div[1]/input')
username.send_keys('TestAccount1')
password = driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/form/div[3]/div/input')
password.send_keys('StackOverflowPass1')
login_button = driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/form/button')
login_button.click()

I have had the same issue with logging in to gmail. I could not solve it for a week. I am 99% sure that you can't bypass the bot detection. How I solved it:
I have created Chrome account to which my gmail will be logged in all the time (disabled 2FA). Then I just start chrome as that user. Bot detection was no more an issue when I was already logged into gmail. Maybe you should try the same, add this command to your driver class:
options.add_argument("user-data-dir=/Users/alexiseggermont/Library/Application Support/Google/Chrome/Default/")
This path is usable for Mac or Linux OS. Also, rename "Default" to your profile id (for me, and in most cases, it was "Profile 1")

Related

selenium automation signing to google cloud throws networks error

I'm using selenium & selenium wire in my project. I'm writing flows to log in google cloud portals..
I enter my google cloud mail then, press on continue in Google sign in and then it log in to gcp.
I got some errors:
Request has invalid authentication credentials. Expected OAuth 2 access token, login cookie or other...
net:: ERR_PROXY_CONNECTION_FAILED
when I do the same flow manually without automation, with the same credentials, it works fine and no any network error.
my web driver
from seleniumwire import webdriver
from seleniumwire.webdriver import ChromeOptions
def test_gcp_flow():
options = ChromeOptions()
options.add_experimental_option("detach", True)
options.add_argument('--no-sandbox')
options.add_argument('--single-process')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--start-maximized")
options.add_argument('--auto-open-devtools-for-tabs')
options.add_argument('--log-level=2')
options.add_argument('--disable-features=IsolateOrigins,site-per-process')
options.add_argument("--ignore_ssl")
options.add_argument('--ignore-ssl-errors')
options.add_argument('--ignore-certificate-errors')
options.add_argument("--disable-extensions")
options.add_argument("--disable-setuid-sandbox")
options.add_argument("--dns-prefetch-disable")
options.add_argument('ignore-certificate-errors')
options.add_argument('disable-web-security')
options.add_argument('--allow-insecure-localhost')
driver = webdriver.Chrome(options=options)
driver.get('....any-hidden-url')
# more flow actions - then it open gcp portal
I added openssl.cnf (without this openssl, it shows me TLS ssl issue) to run it locally in my test using Pycharm:
openssl_conf = openssl_init
[openssl_init]
ssl_conf = ssl_sect
[ssl_sect]
system_default = system_default_sect
[system_default_sect]
Options = UnsafeLegacyRenegotiation
I tried to add some change the chrome options that added to selenium driver but nothing has changed, still same error.
I tried to use Firefox webdriver and it works good without any network issue.
Maybe it's any chrome cache issue? because some days ago it worked me with chrome..
what I'm expecting is that to sign in to gmail without network/token issues.
I can think of two possible options:
Google detects Selenium and prevents login with it for security reasons. Well selenium web driver injects recognizable javascript to the browser, and you can detect it using a simple script.
You have an authentication cookie in your browser that is not used with Selenium.
Each Selenium session initiates itself completely clean. You can save your cookies with pickle for instance and then inject it to Selenium web driver with Python. Although I would avoid it if you cant store it encrypted in a secure place and fetch it during run time (or else - at some point it will be stolen no matter how much you think you are secured).
By the way if its something that you really need - YOU are a paying customer, maybe google has an available solutions for this kind of need.
Good luck!
Try using selenium wire with an undetected chrome-driver.
pip install selenium-wire
pip install undetected-chromedriver
from seleniumwire.undetected_chromedriver.v2 import Chrome, ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
if __name__ == "__main__":
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")
browser = Chrome(seleniumwire_options=options, options=chrome_options)
browser.get('your url')
... your rest of the code
I hope it resolves the issue.

Can we run selenium in our current account logged- in browser?

Can we run selenium in our current account logged in browser ?
Basically if i try logging into google using selenium , its says browser insecure.
i am trying to make a amazon cart auto checkout as a school project
so if i try in my existing browser , my amazon id is already registered and i dont have to sign in again . but if i use amazon login in selenium its asking for signin and 2fa is being sent to my mail id, how to i skip this step and directly go to the logged in page??
please help
You can't use your non-chrome driver browser (aka regular chrome browser). Selenium only works with chrome-drivers. One way remain signed in is to specify a profile in options so that every-time the driver initiates, it loads your cookies and history.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--user-data-dir=Amazon")
driver = webdriver.Chrome(options=chrome_options)
From the code above, chrome_options.add_argument("--user-data-dir=Amazon") will create a profile 'Amazon' if not already there, and save cookies and history there.
The next time you run the driver it will load it from 'Amazon'.
Here is a blog that explains how to install a chrome browser remotely on a specific folder by installing a dedicated browser onto it to use selenium with.
This manages all data including cache, history, accounts, etc
for further info refer the link here
Here is a blog that explains how to install a chrome browser remotely on a specific folder by installing a dedicated browser onto it to use selenium with.
This manages all data including cache, history, accounts, etc
for further info refer the link here
https://learn-automation.com/how-to-execute-selenium-scripts-on-already-opened-browser/

Using selenium to login apple connect always need verify

I use selenium with chromedriver to login apple connect website on ubuntu 20.04 and chrome version 99.0.4844.51
The login page is easy to handle but next it detect the new browser and require device verify code. I think maybe I type once then it won't require but I was wrong.
It still require verifying when I trigger the python script. Is there possible the server location is different with my device? The server is at USA and I'm in Asia.
I found that argument 'profile-directory' is not create the specific directory in chrome-data!
I try the following solution but not working.
save cookies when login success first time and reload cookies next time
add userAgent
add custom profile location
def main():
ua = UserAgent()
userAgent = ua.google
opts = Options()
opts.add_argument('user-agent={userAgent}')
opts.add_argument("user-data-dir=/home/jack/crawler/chrome-data")
opts.add_argument('profile-directory=Profile3')
driver = webdriver.Chrome(options=opts)
driver.get("https://appstoreconnect.apple.com/")
You can follow this post to create a custom profile.
How to open a Chrome Profile through --user-data-dir argument of Selenium
If you are using ubuntu you can do this:
Trying to use remote desktop to connect your ubuntu. (xrdp or others)
Open google chrome and create a profile
type chrome://version to check the profile path
Fill the path and profile name
opts = Options()
opts.add_argument("--user-data-dir=/home/yuyu/.config/google-chrome")
opts.add_argument("--profile-directory=Profile 1")
It worked for me to let website remember the browser

Selenium Google Login Block

I have a problem with Google login. I want to login to my account but Google says that automation drivers are not allowed to log in.
I am looking for a solution. Is it possible to get a cookie of normal Firefox/Chrome and load it into the ChromeDriver/GeckoDriver? I thought that this can be a solution. But I am not sure is it possible or not..
Looking for solutions...
Also, I want to add a quick solution. I solved this issue by
using one of my old verified account. That can be a quick solution for
you.
I had the same problem and found the solution for it. I am using
1) Windows 10 Pro
2) Chrome Version 83.0.4103.97 (Official Build) (64-bit)
3) selenium ChromeDriver 83.0.4103.39
Some simple C# code which open google pages
var options = new ChromeOptions();
options.addArguments(#"user-data-dir=c:\Users\{username}\AppData\Local\Google\Chrome\User Data\");
IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver();
driver = new ChromeDriver(Directory.GetCurrentDirectory(), options);
driver.Url = "https://accounts.google.com/";
Console.ReadKey();
The core problem here you cant login when you use selenium driver, but you can use the profile which already logged to the google accounts.
You have to find where your Chrome store profile is and append it with "user-data-dir" option.
PS. Replace {username} with your real account name.
On linux the user profile is in "~/.config/google-chrome".
This error message...
...implies that the WebDriver instance was unable to authenticate the Browsing Context i.e. Browser session.
This browser or app may not be secure
This error can happen due to different factors as follows:
In the article "This browser or app may not be secure" error when trying to sign in with Google on desktop apps #Raphael Schaad mentioned that, if an user can log into the same app just fine with other Google accounts, then the problem must be with the particular account. In majority of the cases the possible reason is, this particular user account is configured with Two Factor Authentification.
In the article Less secure apps & your Google Account it is mentioned that, 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 Unable to sign into google with selenium automation because of "This browser or app may not be secure."
Deep Dive
However, to help protect your account, Web Browsers may not let you sign in from some browsers. Google might stop sign-ins from browsers that:
Doesn't support JavaScript or have Javascript turned off.
Have AutomationExtension or unsecure or unsupported extensions added.
Use automation testing frameworks.
Are embedded in a different application.
Solution
In these cases there are diverse solutions:
Use a browser that supports JavaScript:
Chrome
Safari
Firefox
Opera
Internet Explorer
Edge
Turn on JavaScript in Web Browsers: If you’re using a supported browser and still can’t sign in, you might need to turn on JavaScript.
If you still can’t sign in, it might be because you have AutomationExtension / unsecure / unsupported extensions turned on and you may need to turn off as follows:
public class browserAppDemo
{
public static void main(String[] args) throws Exception
{
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.setExperimentalOption("useAutomationExtension", false);
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
WebDriver driver = new ChromeDriver(options);
driver.get("https://accounts.google.com/signin")
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='identifierId']"))).sendKeys("gashu");
driver.findElement(By.id("identifierNext")).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#name='password']"))).sendKeys("gashu");
driver.findElement(By.id("passwordNext")).click();
System.out.println(driver.getTitle());
}
}
You can find a couple of relevant discussions in:
Gmail login using selenium webdriver in java
Selenium test scripts to login into google account through new ajax login form
Additional Considerations
Finally, some old browser versions might not be supported, so ensure that:
JDK is upgraded to current levels JDK 8u241.
Selenium is upgraded to current levels Version 3.141.59.
ChromeDriver is updated to current ChromeDriver v80.0 level.
Chrome is updated to current Chrome Version 80.0 level. (as per ChromeDriver v80.0 release notes)
Solution without redirecting, using firefox driver, or changing any google account settings:
If you have a specific Google account you want to access, create a chrome profile with it and then load the chrome profile when using selenium:
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=C:/Users/{userName}/AppData/Local/Google/Chrome/User Data/Profile {#}/")
driver = webdriver.Chrome("C:/bin/chromedriver.exe", chrome_options=options)
Windows:
The profile {#} in the file path above will vary so I suggest checking inside of the User Data folder which profile you want to use. For example, if you currently only have 1 chrome account there will be no Profile directory (resorts to "Default" directory) in User Data but if you create a second chrome account there will be a "Profile 1" directory in User Data.
Note that you should create a new google chrome profile to use with selenium because attempting to use a chrome profile that is already in use (opened in another chrome window) will cause an error.
Mac:
This solution may or may not work on mac but to find the chrome account folder/filepath follow the instructions in the comment left by #bfhaha
One Solution that works for me: https://stackoverflow.com/a/60328992/12939291 or https://www.youtube.com/watch?v=HkgDRRWrZKg
Short: Stackoverflow Login with Google Account with Redirect
from selenium import webdriver
from time import sleep
class Google:
def __init__(self, username, password):
self.driver = webdriver.Chrome('./chromedriver')
self.driver.get('https://stackoverflow.com/users/signup?ssrc=head&returnurl=%2fusers%2fstory%2fcurrent%27')
sleep(3)
self.driver.find_element_by_xpath('//*[#id="openid-buttons"]/button[1]').click()
self.driver.find_element_by_xpath('//input[#type="email"]').send_keys(username)
self.driver.find_element_by_xpath('//*[#id="identifierNext"]').click()
sleep(3)
self.driver.find_element_by_xpath('//input[#type="password"]').send_keys(password)
self.driver.find_element_by_xpath('//*[#id="passwordNext"]').click()
sleep(2)
self.driver.get('https://youtube.com')
sleep(5)
username = ''
password = ''
Google(username, password)
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.
This might be still open / not answered
Here is an working (28.04.2021) example in this following thread:
https://stackoverflow.com/a/66308429/15784196
Use Firefox as driver. I tested his example and it did work!
#Mike-Fakesome on this https://gist.github.com/ikegami-yukino/51b247080976cb41fe93 thread suggest a solution that works
import undetected_chromedriver.v2 as uc
import random,time,os,sys
from selenium.webdriver.common.keys import Keys
GMAIL = '<GMAIL_HERE>'
PASSWORD = '<PASSWORD_HERE>'
chrome_options = uc.ChromeOptions()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument("--profile-directory=Default")
chrome_options.add_argument("--ignore-certificate-errors")
chrome_options.add_argument("--disable-plugins-discovery")
chrome_options.add_argument("--incognito")
chrome_options.add_argument("user_agent=DN")
driver = uc.Chrome(options=chrome_options)
driver.delete_all_cookies()
driver.get("https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&prompt=consent&response_type=code&client_id=407408718192.apps.googleusercontent.com&scope=email&access_type=offline&flowName=GeneralOAuthFlow")
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[1]/input").send_keys(GMAIL)
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[1]/input").send_keys(Keys.RETURN)
time.sleep(10)
also you can use import undetected_chromedriver as uc instead of import undetected_chromedriver.v2 as uc now as well
I solved this issue last week using following steps:
First two steps are out of your project code.
Create a new user directory for Chrome browser.
You can name this folder whatever you like and place it anywhere.
Run Chrome browser in debugger mode using just created directory
cd C:\Program Files\Google\Chrome\Application
chrome.exe --remote-debuggin-port=9222 --user-data-dir="C:\localhost"
You can use any free port but I followed this article:
https://chromedevtools.github.io/devtools-protocol/
Browser window opens.
Login manually to Google / Facebook / etc using opened window.
Close the browser.
In your project:
Copy chrome-user-directory you just created into 'resources' package.
Set debugging option for Chrome driver.
/**
* This method is added due to Google security policies changed.
* Now it's impossible to login in Google account via Selenium at first time.
* We use a user data directory for Chrome where we previously logged in.
*/
private WebDriver setWebDriver() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir=" + System.getProperty("user.dir") + "/src/main/resources/localhost");
options.addArguments("--remote-debugging-port=9222");
return new ChromeDriver(options);
}
Enjoy.
PS: If you have another solution without copying chrome user-directory into the project, please share it)
I found a solution ,#theycallmepix and #Yinka Albi are correct but because(i think) google blacklisted accounts that did just programatically login the first time and so later they coudn't login normally. So Basically just use different a account and go to to Stackauth or StackoverFlow. Then manually login with Google(first link your account) And then manually login in google.com and then it should work prgramaticaly
P.S. pls comment if this doesn't work
Use the below given snippet method to Login to your Google Account.
Language: Python3
Redirect via: StackAuth (Reason explained at the end)
[Edit: You need to import the required packages. Make sure that the Automation that you do is running in Foreground, I mean, it's not minimised until you login completely. Once if the login is successful, then you can re-direct to the required website that you want.]
def login(username, password): # Logs in the user
driver.get('https://accounts.google.com/o/oauth2/auth/identifier?client_id=717762328687-iludtf96g1hinl76e4lc1b9a82g457nn.apps.googleusercontent'
'.com&scope=profile%20email&redirect_uri=https%3A%2F%2Fstackauth.com%2Fauth%2Foauth2%2Fgoogle&state=%7B%22sid%22%3A1%2C%22st%22%3A%2'
'259%3A3%3Abbc%2C16%3A561fd7d2e94237c0%2C10%3A1599663155%2C16%3Af18105f2b08c3ae6%2C2f06af367387a967072e3124597eeb4e36c2eff92d3eef697'
'1d95ddb5dea5225%22%2C%22cdl%22%3Anull%2C%22cid%22%3A%22717762328687-iludtf96g1hinl76e4lc1b9a82g457nn.apps.googleusercontent.com%22%'
'2C%22k%22%3A%22Google%22%2C%22ses%22%3A%2226bafb488fcc494f92c896ee923849b6%22%7D&response_type=code&flowName=GeneralOAuthFlow')
driver.find_element_by_name("identifier").send_keys(username)
WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.XPATH, "//*[#id='identifierNext']/div/button/div[2]"))).click()
driver.implicitly_wait(4)
try:
driver.find_element_by_name("password").send_keys(password)
WebDriverWait(driver, 2).until(expected_conditions.element_to_be_clickable((By.XPATH, "//*[#id='passwordNext']/div/button/div[2]"))).click()
except TimeoutException:
print('\nUsername/Password seems to be incorrect, please re-check\nand Re-Run the program.')
del username, password
exit()
except NoSuchElementException:
print('\nUsername/Password seems to be incorrect, please re-check\nand Re-Run the program.')
del username, password
exit()
try:
WebDriverWait(driver, 5).until(lambda webpage: "https://stackoverflow.com/" in webpage.current_url)
print('\nLogin Successful!\n')
except TimeoutException:
print('\nUsername/Password seems to be incorrect, please re-check\nand Re-Run the program.')
exit()
The above code, takes 2 parameters - gmailID and password. If the password or username is wrong, then you'll notified.
Why stackauth?
-> Stackauth uses OAuth 2.0 authorisation to access Google APIs(here, Google account login needs Google API to work) to securely login a user into his/her Google Account.
Click here to read more about OAuth.
Edit:
I just answered to my own question which I'd posted yesterday thinking that it might help you.
As of now, 2021, it can successfully bypass all the google restrictions that used to occur when logging in.
Feel free to revert back if it doesn't work.
Link to my answer is here
If your Chrome browser was spun up using Chromedriver, then there is detectable evidence that websites can use to determine if you're using Selenium, and then they can block you. However, if the Chrome browser is spun up before Chromedriver connects to it, then you have a browser that no longer looks like an automation-controlled one. Modern web automation libraries such as undetectable-chromedriver are aware of this, and so they make sure Chrome is spun up before connecting chromedriver to it.
The modern framework that I use for these situations is SeleniumBase in undetected chromedriver mode. Here's a script that you can use to get past automation detection on Google: (Run with python after installing seleniumbase with pip install -U seleniumbase)
from seleniumbase import SB
with SB(uc=True) as sb:
sb.open("https://www.google.com/gmail/about/")
sb.click('a[data-action="sign in"]')
sb.type('input[type="email"]', "NAME#gmail.com")
sb.click('button:contains("Next")')
sb.type('input[type="password"]', PASSWORD)
sb.click('button:contains("Next")')
sb.sleep(5)
A slow yet good solution would be delaying every key press. Why? because google uses a kind of captcha where it analyzes your typing speed and more things. So if you wanna type a mail or password like example#example.com, you'd have to do this:
for i in "example#example.com\n": #\n because the submit doesn't work in input fields in google sign in, so \n is equivalent of pressing enter
element.send_keys(i)
time.sleep(0.4) #don't forget to import time or something else with which you could delay your code!
time.sleep(1) #also this sleep because when the button will redirect url, it'd not reload the site and selenium will not wait so one more sleep
PS: if not working, try changing the values of sleep or any other delaying function

Sign in to gmail account fails (selenium automation)

I have a Selenium service that has to login to my gmail account as the first step. This functionality was working couple of weeks ago, but suddenly the login starts to fails and i am seeing this Error in browser, tried both in Chrome and Firefox drivers in selenium -
This Error comes after the selenium service inserts the email,password and clicks on the sign in button. A similar error was also reported in Google support Forum here- https://support.google.com/accounts/thread/10916318?hl=en, They said that "Google seems to have introduced automation tools detection on their login flow!" but there is no solution in this thread.
Some Other Details which might be useful-
I am not able to login manually to Google accounts in the browsers
opened by Selenium.
But I am able to login manually to these accounts in the Google
Chrome application.
Let me know if you need to take a look at the code, i will post it here.
Thanks in Advance!
Edit
Adding Sample code to refer.
public void loginGoogleAccount(String emailId, String password) throws Exception {
ChromeOptions options = new ChromeOptions();
options.addArguments("--profile-directory=Default");
options.addArguments("--whitelisted-ips");
options.addArguments("--start-maximized");
options.addArguments("--disable-extensions");
options.addArguments("--disable-plugins-discovery");
WebDriver webDriver = new ChromeDriver(options);
webDriver.navigate().to("https://accounts.google.com");
Thread.sleep(3000);
try {
WebElement email = webDriver.findElement(By.xpath("//input[#type='email']"));
email.sendKeys(emailId);
Thread.sleep(1000);
WebElement emailNext = webDriver.findElement(By.id("identifierNext"));
emailNext.click();
Thread.sleep(1000);
WebDriverWait wait = new WebDriverWait(webDriver, 60);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("identifierNext")));
Thread.sleep(3000);
WebElement passwordElement = webDriver.findElement(By.xpath("//input[#type='password']"));
passwordElement.sendKeys(password);
Thread.sleep(1000);
WebElement passwordNext = webDriver.findElement(By.id("passwordNext"));
passwordNext.click();
} catch (Exception e) {
LOGGER.info(String.format("No email/password field available or it is already logged in: [%s]: ",
e.getMessage()));
}
}
Toggle "Allow Less Secure App Access"
There is a setting on your account that you can toggle that may help with this. It is the "Allow Less Secure App Access". You should be able to visit the link below to toggle that setting if you are already logged into the gmail account you want to modify.
Link to change setting on google account:
https://myaccount.google.com/lesssecureapps
Further information(source):
https://support.google.com/accounts/answer/6010255
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.
This issue was because of the selenium chrome profile. Create a new chrome profile and logged into it with the email id with which you were facing the issue. Then Turn on sync.
With this chrome profile in place I can skip the login steps and directly do the main process. Use: Chrome Options to add newly created chrome profile as an argument.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("Binary path of the Chrome");
Hope this one helps you.
Check if your Chrome version is >= 79 and if so, downgrade to 78. Here's what I did (Windows):
I uninstalled "retail Chrome" which constantly kept upgrading itself to the latest version using sneaky tricks such as Google Chrome services that would check for updates in the background.
Even disabling those system services didn't help because retail Chrome also installs timer events that would re-enable said services in the middle of the night, so you'd wake up to a new version, and not even notice until things break.
I installed v78 from the "offline installer" found here, which doesn't seem to install any "helpful" auto upgrade features: https://www.neowin.net/news/google-chrome-780390470-offline-installer/
The above problem went away like magic. It appears that v79 has some anti-feature built in that calls home with information that allows Google to conclude that a bot is at work.
Hope this works for you... if not, you could invest a lot of time and create your own "Chrome simulator" by patching and compiling Chromium accordingly...
The only one solution which worked for me, just create fresh random google account, and that's it. Worked without any problems.
I fixed it by using selenium-stealth module and editing chromedriver.exe.
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import time
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium_stealth import stealth
chrome_options = Options()
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches",["enable-automation"])
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument('--disable-logging')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
caps = DesiredCapabilities.CHROME
caps['goog:loggingPrefs'] = {'performance': 'ALL'}
# for editing chromedriver exe so that its not detected(run only once)
with open("chromedriver.exe","rb") as d:
file_data=d.read()
file_data=file_data.replace(b"cdc_",b"tch_")
with open("chromedriver.exe","wb") as d:
d.write(file_data)
driver = webdriver.Chrome('chromedriver.exe',chrome_options=chrome_options,desired_capabilities=caps)
# for injecting js to that tab so that browser not detected
stealth(driver,languages=["en-US", "en"],vendor="Google Inc.",platform="Win32",webgl_vendor="Intel Inc.",renderer="Intel Iris OpenGL Engine",fix_hairline=True,)
driver.get("https://accounts.google.com")
time.sleep(3)
driver.switch_to.active_element.send_keys("myemail#gmail.com\n")
time.sleep(3)
driver.switch_to.active_element.send_keys("mypassword\n")
This works for me:
from selenium import webdriver
from time import sleep
username=raw_input("username: ")
password=raw_input("password: ")
driver=webdriver.Chrome('...')
driver.get('https://stackoverflow.com/users/signup')
sleep(3)
driver.find_element_by_xpath('//*[#id="openid-buttons"]/button[1]').click()
driver.find_element_by_id('identifierId').send_keys(username)
driver.find_element_by_id('identifierNext').click()
sleep(3)
driver.find_element_by_name('password').send_keys(password)
driver.find_element_by_id('passwordNext').click()
sleep(2)
driver.get('https://mail.google.com/mail/u/0/#inbox')
I found a similar solution here.