【问题标题】:How to send keys in Google Document using Selenium如何使用 Selenium 在 Google 文档中发送密钥
【发布时间】:2018-10-11 18:19:54
【问题描述】:

我正在尝试使用 Selenium 自动创建 Google 文档并向其中添加随机文本。我在程序中创建了一个名为 doccontent 的列表,它从中提取以进入 Google 文档的正文,但我似乎无法让它工作。这是我的代码的一部分:

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)

返回错误:

selenium.common.exceptions.WebDriverException:消息:未知错误:无法聚焦元素 (会话信息:chrome=69.0.3497.100) (驱动信息:chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 10.0.17134 x86_64)

编辑:

刚刚改成:

#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)

这是尝试tab进入文档,但还是报错:

selenium.common.exceptions.WebDriverException:消息:未知错误:无法聚焦元素 (会话信息:chrome=69.0.3497.100) (驱动信息:chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 10.0.17134 x86_64)

【问题讨论】:

  • 哪一行代码出错了?
  • 从设计上讲,使用 Selenium 自动化 Google 将非常困难。你应该看看官方 API:developers.google.com/drive/api/v3/about-sdk
  • @venkatesh.b body.send_keys(docwords)
  • @SiKing 我已经能够添加文档并重命名它们,这只是我卡住的部分
  • 对我来说,它已经在 driver.find_element_by_xpath('//*[@id=":1d"]/div[1]') 行失败了 - 该 ID 存在,但位于样式包含 visibility:none 的父级下,因此 selenium 无法点击它。

标签: python selenium webdriver


【解决方案1】:

我也遇到过同样的情况。这就是我能够将文本发送到 Google Doc 的方式:

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

编辑: 经过一些测试后,您应该确保在页面完全加载之前留出一些时间并插入正文向文档添加标题

【讨论】:

    【解决方案2】:

    这是我用来向 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()
    

    上面的 ActionChains 行是允许我们这样做的,所以利用它

    【讨论】:

      猜你喜欢
      • 2019-10-13
      • 2021-01-01
      • 1970-01-01
      • 2022-11-10
      • 2020-07-30
      • 2017-05-07
      • 2021-04-21
      • 1970-01-01
      • 2018-07-24
      相关资源
      最近更新 更多