Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

(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("[email protected]")
driver.find_element_by_id("identifierNext").click()

enter image description here

like image 827
ibexy Avatar asked Mar 07 '26 23:03

ibexy


1 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

  • “This browser or app may not be secure” error while attempting to login in to Gmail account using Selenium

  • GMail is blocking login via Automation (Selenium)

  • Trying to login to Gmail with Selinum but “This browser or app may not be secure”

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.

like image 98
Life is complex Avatar answered Mar 10 '26 15:03

Life is complex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!