如何解决从谷歌工作表和硒中提取数据以写入网页
from __future__ import print_function
from googleapiclient.discovery import build
from google.oauth2 import service_account
from selenium import webdriver
import time
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SPREADSHEET_ID,range="TAX!A1:E5").execute()
data = result.get('values')
list = []
for i in user_input:
if i == 1:
list.append(data[0])
elif i == 2:
list.append(data[1])
elif i == 3:
list.append(data[2])
elif i == 4:
list.append(data[3])
elif i == 5:
list.append(data[4])
else:
print('ERROR')
print(list)
print(('The length of list is: ' + str(len(list)))
print(range(len(list)))
driver = webdriver.Safari()
try:
driver.get('some_website')
for i in range(len(list)):
search_Box = driver.find_element_by_xpath('some_search_Box')
search_Box.click()
search_Box.send_keys(list[i])
cart_button = driver.find_element_by_xpath('some_button')
cart_button.click()
#print any exceptions such as element not found error,then close browser
except Exception as e:
print(e)
print('driver closing on error')
driver.close()
这是我得到的:
[['1','2','3','4','5'],['2','5','6'],['6','4']]
列表长度为:3 范围(0,3)
用户输入一个或多个选项,并根据该选项从工作表中提取可能是多个值的数据。这就是列表的形式。
它可以正常工作,但有一个问题。它在搜索框中键入整个第一个数组 (['1','5']) 但我希望它搜索每个元素。 我希望列表的长度是 14 而不是 3,我不知道如何解决这个问题。
是否可以附加到列表并在输出上获得一个数组,数组的长度取决于用户输入?
解决方法
for i in range(len(list)):
# Add this line to loop through the second element and so forth.
for j in range(len(list[i])):
# Fix this one
search_box.send_keys(list[i][j])
只需使用 for for 循环遍历二维列表即可。
你也可以简化顶层
if i<=5 and i >=1:
list.append(data[i-1])
else:
print('ERROR')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。