【问题标题】:How to Iterate through data present in the spread sheet in selenium/data如何遍历硒/数据中电子表格中存在的数据
【发布时间】:2019-07-30 14:44:13
【问题描述】:

我正在自动化一个网页,其中数据从电子表格输入到网页中,例如姓名,出生日期。如果电子表格中有一条记录,我可以运行。问题是我不知道如何如果我在电子表格中有 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);

【问题讨论】:

    标签: python excel selenium selenium-webdriver


    【解决方案1】:

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

    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) 
    

    【讨论】:

    • 我也从网页中获取一个值并根据每条记录将其存储在excel中,所以上述方法也可以在这种情况下工作。
    • 如果您知道行数,那么它应该可以正常工作。确保循环始终从 1 开始,因为 0 是标题元素。
    • 将数据从网页存储到 excel pandas 库是另一个不错的选择。
    猜你喜欢
    • 1970-01-01
    • 2022-07-11
    • 2011-10-28
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2010-12-08
    • 2010-11-03
    • 1970-01-01
    相关资源
    最近更新 更多