How to open Google Sheet Doc in Chrome browser using Python?

Viewed 1179

(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

2 Answers

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.

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.

Related