【问题标题】:Automated test Python/selenium gspread自动化测试 Python/selenium gspread
【发布时间】:2016-12-01 17:23:06
【问题描述】:

例如,要查看 Google 表格文档,我可以使用 gspread

import gspread

gc = gspread.login("your login", "your password")
wks = gc.open("document name").worksheet("sheet name")

cellValue = wks.acell("A1").value
print "Cell 'A1' has a value of %s" % cellValue

有没有一种方法可以访问 Google 表格并尝试使用 Python 和 Selenium 在自动化测试中对其进行编辑?

【问题讨论】:

  • 请澄清“访问谷歌表格并尝试使用 python 和 selenium 在自动化测试中对其进行编辑”

标签: python selenium google-sheets


【解决方案1】:

https://developers.google.com/people/quickstart/python

  • 转到上面的链接并按照您的谷歌帐户提供的步骤创建一个项目。

  • 您将从上面的链接下载 OAuth 客户端 ID,它会为您提供 client_secret.json。

  • 在此示例中,未使用 OAuth 客户端 ID,在使用其他 api 时需要。

  • 除上述之外,您还必须下载 服务帐户密钥,该密钥位于 OAuth 客户端 ID 下方。以 json 格式下载。

  • Service Account Key 的路径必须在以下代码的第四行中提及。

代码:

import gspread
from oauth2client.service_account import ServiceAccountCredentials
#scope is necessary if we are accesing gdrive
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('/home/username/Downloads/MyProject.json', scope)
gc = gspread.authorize(credentials)

#to create a new gsheet
sh = gc.create('Sheet_1')

#to open existing gsheet
sh = gc.open('LYKE')

# to share to other users
sh.share('username@gmail.com', perm_type='user', role='writer')

#to create a worksheet in the gsheet
worksheet = sh.add_worksheet(title='title', rows="500", cols="50")

#Define a cell range and update cells
cell_list = worksheet.range('A1:A10')
for i,cell in enumerate(cell_list):
    cell.value=i
worksheet.update_cells(cell_list)

【讨论】: