如何遍历硒/数据中电子表格中的数据

时间:2019-07-30 14:44:13

标签: python excel selenium selenium-webdriver

我正在自动化一个网页,在该网页中,数据是从电子表格中输入的,例如姓名,出生日期。如果电子表格中只有一条记录,我能够运行。问题是我不知道如何如果我在电子表格中有n个数据,则要遍历电子表格。

下面,到目前为止,我能够读取1条记录。我想同时读取两条记录1&2

Example as in spread sheet:
 Record  Name   DOB
    1    TEST1  05/06/2010
    2    TEST2  06/05/2010

请找到我到目前为止尝试过的代码

driver = webdriver.Chrome('path')
driver.fullscreen_window();
driver.get('url');
time.sleep(5);
Workbook=xlrd.open_workbook("excelpath")
Customerdetails = Workbook.sheet_by_index(0);
Storagezipcode = Customerdetails.cell(1,0);
text_area=driver.find_element_by_xpath('(//*[@id="QuoteDetails/PostalCode"])[2]');
text_area.send_keys(Storagezipcode.value);
Nextbutton=driver.find_element_by_xpath('//span[contains(text(),"Next")]');
Nextbutton.click()
time.sleep(10)
#carlink page
CarLink=driver.find_element_by_xpath('//span[@class="link"]');
CarLink.click();
time.sleep(30)
#ModelYear page
yeardetail=Customerdetails.cell(1,14);
Yearlink = driver.find_element_by_xpath("//span[normalize-space(.)='{}']".format(yeardetail.value));
Yearlink.click();
time.sleep(10)
#Company name
SubModel=Customerdetails.cell(1,15);
SubModellink=driver.find_element_by_xpath("//span[normalize-space(.)='{}']".format(SubModel.value));
SubModellink.click();
time.sleep(10)
#Company model
Bodymodel=Customerdetails.cell(1,16);
Bodylink=driver.find_element_by_xpath("//span[normalize-space(.)='{}']".format(Bodymodel.value));
Bodylink.click();
time.sleep(10);

1 个答案:

答案 0 :(得分:0)

使用行数获取所有行,然后迭代并使用列索引。

import xlrd
Workbook=xlrd.open_workbook("excelpath")
Customerdetails = Workbook.sheet_by_index(0)
for i in range(1,Customerdetails.nrows):
    Record=Customerdetails.cell(i,0)
    print(Record.value)
    Name= Customerdetails.cell(i,1)
    print(Name.value)
    DOB = Customerdetails.cell(i, 2)
    print(DOB.value)