当我使用python中的def函数从excel表中提取数据时,我得到单引号和双引号(“”,“,[])

时间:2017-10-20 07:49:36

标签: python excel selenium selenium-webdriver selenium-chromedriver

当我在python selenium中使用def函数从excel表中提取数据时,我得到单引号和双引号的输出(“”,“',[])。我不想在单引号或双引号中输出。

另外,当我使用selenium从“Gmail应用程序”中的Excel工作表中读取数据时,它会读入单引号和双引号。

请你帮我解决这个问题。

from selenium import webdriver
from time import sleep
from xlrd import open_workbook
import xlrd
import xlwt



book=xlrd.open_workbook("E:\\Email.xlsx")
print(book.nsheets)
SheetName =book.sheet_by_name("gmail")
rows=SheetName.nrows
cols=SheetName.ncols
def getcell(rows,cols):

    table=list()
    record=list()

    for x in range(rows):
        for y in range(cols):
            record.append(SheetName.cell(x,y).value)    
            table.append(record)
            record=[]
            rows=rows+1

    return table;

v=getcell(1,1)
print(str(v))

输出:

[[ 'ABCDEFG']]

[[ 'abc@gmail.com']]

driver = webdriver.Chrome("C:\\Users\\home\\workspace\\geeko\\chromedriver.exe")
driver.maximize_window()
driver.get("https://www.gmail.com/")
elem=driver.find_element_by_xpath("//*@id='identifierId']").send_keys(str(getcell(1, 1)))

1 个答案:

答案 0 :(得分:0)

确定将此函数添加到您的代码中(注意它会将表连接在一个字符串中,但您可以使用您需要的任何分隔符对其进行修改

def strip_table(t):
    out_str = ""
    for item in t:
        out_str += str(item[0])

    return out_str

然后替换

elem=driver.find_element_by_xpath("//*@id='identifierId']").send_keys(str(getcell(1, 1)))

elem=driver.find_element_by_xpath("//*@id='identifierId']").send_keys(strip_table(getcell(1, 1)))