3

(Question Edited) I am using the below code to try and open an already existing Google sheet on my Chrome browser. The requirement is to open the sheet in a browser.

Am not sure Selenium would be the best option though. Is there a different way to authenticate and access the/open the doc in the browser?

For now I am trying to use Selenium for the exercise. The code below opens the browser and Login screen. After entering ID and clicking the [Next] button, I hit the screen below. Cant figure out how to bypass this?

Here is the code (with corrections suggested by: @Priyank Vekariya):

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

#open Google logon
driver.find_element_by_name("identifier").send_keys("My_Email_@abc.com")
driver.find_element_by_id("identifierNext").click()

enter image description here

ibexy
  • 609
  • 3
  • 16
  • 34

2 Answers2

2

UPDATE

I have attempted to access Google Sheets by logging directly in to Gmail or YouTube via Selenium using Google Chrome, Microsoft Edge and Mozilla Firefox. I also tried to use a side-channel Google login through Stack Overflow and other services. Only once I was able to get far enough, before I was thrown under the crazy captcha bus.

After all these failures, I decided to gave this problem more thought. I determined that you didn't need to log in to Google to access the document in Google Drive. You just need to share the Google Sheet from the account that controls the document with the permissions needed to read the sheet with Python.

enter image description here

Once the sheet is shared you can access and query it using Selenium.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")

# disable the banner "Chrome is being controlled by automated test software"
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])

driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=chrome_options)
driver.get('https://docs.google.com/spreadsheets/d/<google provided random string>/edit?usp=sharing')

Here is the shared Google Sheet, which didn't require a login to access or edit.

enter image description here

ORIGINAL POST


Google forbids using automated scripts, such as Selenium for logging into Gmail.

enter image description here

When you click Learn More you get this message.

enter image description here

Please note this line: Are being controlled through software automation rather than a human

YOU CAN BYPASS THIS, but you have to login through another portal, such as Stack OverFlow that will allow you to use your Gmail credentials to login. Once that happens you can access your Google Sheet.

If you need help with this please let me know and I will try to put some code together for you.

Life is complex
  • 15,374
  • 5
  • 29
  • 58
  • Please give me an example. Thanks for your help. We meet again here :) – ibexy Feb 23 '21 at 14:59
  • 1
    I'm working on an example, but it seem that Google has added more capability to detect Selenium logins even through Stack OverFlow. When I tried the Stack Overflow route I'm getting various captchas, which have to be bypassed. – Life is complex Feb 23 '21 at 15:06
  • Here are some more [details on this complex issue] (https://gist.github.com/ikegami-yukino/51b247080976cb41fe93). At the moment I cannot bypass the login issue. I'll keep looking at the problem for you. – Life is complex Feb 23 '21 at 16:57
  • 1
    I posted a work around. Please let me know if it works for your use case. – Life is complex Feb 23 '21 at 23:09
  • your solution works excellently! Once again thanks for your help! – ibexy Feb 24 '21 at 10:50
  • @ibexy You're welcome. I'm glad that I was able to help you. Happy coding!! – Life is complex Feb 24 '21 at 13:17
0

You should verify name of the tag in html code for the page. When I checked the name for the element, it is different for this element which you are trying to find(Email or phone). It shows name as "identifier" in my system. Either you can try by id of that element. So, you should replace below code. Maybe it will help.

driver.find_element_by_name("identifier").send_keys("My_Email_@abc.com")

Thank you.

  • Thanks for your answer but after login, I now encounter a screen that that says: "Couldn't sign you in This browser or app may not be secure. Learn more Try using a different browser. If you’re already using a supported browser, you can refresh your screen and try again to sign in.". I have edited the question to include this. – ibexy Feb 22 '21 at 17:44
  • Okay. Thanks for responding. – Priyank Vekariya Feb 24 '21 at 05:39