【发布时间】: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 无法点击它。