0

I'm trying to use Selenium to automate creating Google Documents and adding random text to them. I've created a list called doccontent in the program that it pulls from to enter into the body of the Google docs but I can't seem to get it to work. Here is part of my code:

import random
from selenium import webdriver
import time

driver = webdriver.Chrome()

#creating doc
driver.get("https://docs.google.com/document/u/0/")
time.sleep(random.randint(1,2))
newdoc = driver.find_element_by_xpath('//*[@id=":1d"]/div[1]')
newdoc.click()
time.sleep(2)
#adding random doc name

rename = driver.find_element_by_class_name('docs-title-input')
rename.click()
docname = "test" + str(random.randint(1,600))
rename.send_keys(docname)

body = driver.find_element_by_class_name('kix-lineview')
time.sleep(1)
docwords = random.choice(doccontent)
body.send_keys(docwords)

It comes back with the error:

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element (Session info: chrome=69.0.3497.100) (Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 10.0.17134 x86_64)

Edit:

Just changed it to:

#creating doc
driver.get("https://docs.google.com/document/u/0/")
time.sleep(random.randint(1,2))
newdoc = driver.find_element_by_xpath('//*[@id=":1d"]/div[1]')
newdoc.click()
time.sleep(2)
#adding random doc name
rename = driver.find_element_by_class_name('docs-title-input')
rename.click()
docname = "test" + str(random.randint(1,600))
rename.send_keys(docname)

body = driver.find_element_by_class_name('kix-page-column')
time.sleep(1)
docwords = random.choice(doccontent)
actions = ActionChains(driver)
actions.send_keys(Keys.TAB * 15)
actions.perform()
time.sleep(1)
body.send_keys(docwords)

This is to try and tab into the document, but it still gives an error:

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element (Session info: chrome=69.0.3497.100) (Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 10.0.17134 x86_64)

Py.R
  • 1
  • 2
  • Which line of the code is giving the error? – venkatesh .b Oct 11 '18 at 18:47
  • 2
    Automating Google using Selenium is going to be very difficult, by design. You should look at the official API: https://developers.google.com/drive/api/v3/about-sdk – SiKing Oct 11 '18 at 18:54
  • @venkatesh.b the body.send_keys(docwords) – Py.R Oct 11 '18 at 21:10
  • @SiKing I have been able to add documents and rename them, it's just this part I am stuck on – Py.R Oct 11 '18 at 21:11
  • for me it's failing already at line `driver.find_element_by_xpath('//*[@id=":1d"]/div[1]')` - that ID exists, but is located under parent whose style contains `visibility:none` so selenium can't click on it. – timbre timbre Oct 11 '18 at 22:22
  • You an try a couple things. One thing you could try is to use sendKeys to send TAB keys to whatever element is active until you are in the doc, then sendKeys again once the google doc input element is active. Another slightly ugly option is to manipulate the DOM via browser.execute (In Javascript anyway) to modify the style of the parent element. – Aaron Oct 12 '18 at 01:00
  • @KirilS. Weird, it seems to work for me. – Py.R Oct 12 '18 at 18:30
  • @Aaron Not sure what you mean by 'send TAB keys', how would I do this in what I have made so far? New to selenium :( – Py.R Oct 12 '18 at 18:31
  • @Aaron Please check the edit I made to the question, is this what you meant? – Py.R Oct 13 '18 at 13:51
  • @Py.R - You need to establish whether or not your elements are existing/visible/clickable by selenium, before taking action on them. Styling of the element you are targeting, as well as the styling of the parent element of that target, can impact what you're trying to do. In some cases, I've had to forcefully manipulate the DOM and styling of some elements to get automation to work. https://stackoverflow.com/questions/7794087/running-javascript-in-selenium-using-python – Aaron Oct 13 '18 at 22:17
  • @Aaron Just tried: strJavaScript=("document.getElementsByClassName('kix-page-column')[0].click();") driver.execute_script(strJavaScript) Still the same error – Py.R Oct 14 '18 at 14:14

2 Answers2

0

I've had the same situation. This is how I was able to get text to the Google Doc:

doc_text = driver.find_element_by_xpath('//body')
doc_text.send_keys('This should be now in the Google Doc's Body')

EDIT: After some testing you should make sure, that you give some time till the page is fully loaded and insert your body text before you add a title to the document

shiny
  • 658
  • 5
  • 8
0

Here is the code I used to send text to Google Docs:

#Import ActionChains
from selenium.webdriver.common.action_chains import ActionChains

#Find element on page(Use (Shift + Right Click) to inspect element)
doc = driver.find_element_by_xpath("insert xpath here")

Wait for a Little Bit
driver.implicitly_wait(10)

#Move to element, click it, and then send text to where your cursor is
ActionChains(driver).move_to_element(doc).click(doc).send_keys("hi").perform()

The ActionChains line right above is what allows us to do it, so leverage that