对于selenium python中的循环脚本,值未保存在Excel中

时间:2016-03-26 08:13:08

标签: python selenium-webdriver

请找到以下脚本。计划在excel中保存Id和Name字段。我没有看到这些值存储在excel中。相反,先前的值将显示在循环调用内的excel中。请通过发布您的建议来帮助我。在此先感谢

脚本:

from selenium.common.exceptions import NoSuchElementException
def run():
    from selenium import webdriver
    from texttable import len
    import xlrd
    import xlwt
    driver=webdriver.Firefox()
    driver.implicitly_wait(10)
    driver.get("http://ssmatri.com/ssnmlprofilelist.php?type=GO")
    driver.maximize_window()
    w1=driver.window_handles[0]
    try:
        while (driver.find_element_by_link_text("1").text=='1'):
            try:
                #driver.find_element_by_id("withphoto").click()
                j=(len("//*[@id='p7']/div[1]/div[3]/div[1]/table[4]/tbody/tr/td"))
                driver.implicitly_wait(20)
                for i in range (2,j+1):
                    name= driver.find_element_by_xpath("//*[@id='p7']/div[1]/div[3]/div[1]/table[4]/tbody/tr[%d]/td[3]"%i).text
                    print driver.find_element_by_xpath("//*[@id='p7']/div[1]/div[3]/div[1]/table[4]/tbody/tr[%d]/td[3]"%i).text
                    Id= driver.find_element_by_xpath("//*[@id='p7']/div[1]/div[3]/div[1]/table[4]/tbody/tr[%d]/td[2]"%i).text
                    print driver.find_element_by_xpath("//*[@id='p7']/div[1]/div[3]/div[1]/table[4]/tbody/tr[%d]/td[2]"%i).text
                    #print driver.find_element_by_xpath("//*[@id='p7']/div[1]/div[3]/div[1]/table[4]/tbody/tr[%d]/td[5]/h5"%i).text
                    print driver.find_element_by_xpath("//*[@id='p7']/div[1]/div[3]/div[1]/table[4]/tbody/tr[%d]/td[1]"%i).text
                    driver.find_element_by_xpath("//*[@id='p7']/div[1]/div[3]/div[1]/table[4]/tbody/tr[%d]/td[1]"%i).click()
                    driver.implicitly_wait(5)
                    w2=driver.window_handles[1]
                    driver.switch_to_window(w2)
                    #print driver.current_url
                    print driver.find_element_by_xpath("//*[@id='txtByJquery']/div/div[3]/div[1]/div[1]/div/table[2]/tbody/tr/td[1]/div[1]/table/tbody/tr[1]/td[3]").text
                    print driver.find_element_by_xpath("//*[@id='txtByJquery']/div/div[3]/div[1]/div[1]/div/table[2]/tbody/tr/td[1]/div[1]/table/tbody/tr[2]/td[3]").text
                    k=driver.find_element_by_xpath("//*[@id='txtByJquery']/div/div[3]/div[1]/div[1]/div/table[2]/tbody/tr/td[1]/div[2]/table/tbody/tr[8]/td[3]").text
                    print k
                    if(k=='H1B Visa'):
                        print 'The person holds passport and H1B Visa'
                    elif(k=="Passport Holder"):
                        print 'The Person holds Passport but does not hold H1B Visa'
                    else:
                        print 'The person does not hold Passport and Visa'
                    driver.close()
                    driver.switch_to_window(driver.window_handles[0])
                    driver.implicitly_wait(5)
                    book=xlwt.Workbook()
                    sheet1=book.add_sheet("Matchings")
                    sheet1.write(i,0,name)
                    sheet1.write(i,1,Id)
                    book.save('C:\Users\HOME\Desktop\saisankara_Matchings.xls')
                    driver.implicitly_wait(5)
                    continue
            except NoSuchElementException:
                driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
                driver.implicitly_wait(5)
            if(driver.find_element_by_link_text("next>>").text=='next>>'):
                driver.find_element_by_link_text("next>>").click()
            elif driver.find_element_by_link_text("1").text=='1' :
                driver.find_element_by_link_text("1")
                #driver.find_element_by_link_text("1").click()
            continue
    except NoSuchElementException:
        driver.quit()

1 个答案:

答案 0 :(得分:0)

因为您在>循环中创建并保存工作簿 - 它会在下一次迭代中覆盖每个先前创建和保存的文件。