selenium:如何将值用作 excel 文件中的变量

selenium : how Use values as variables from excel file

我正在尝试学习编程,我需要你的帮助来解决这个问题,我在 excel 文件中有一些值,大约 2000 行,我想将这些值作为变量添加到我的代码中循环直到最后一行

我的思路如下:

#my_Excel file 
MyDATA=pd.read_excel(r'C:\Users \Desktop\Project\NEWDATA.xlsx')

#The columns contains values
columns=[['ID','CourseCode','Fromdate','ToDate','Loction']]


#required to use the values in the columns
driver.find_element_by_id("m_m_cBody_bdy_uc_tbl_s_from_date").send_keys(str(Fromdate))
driver.find_element_by_id("m_m_cBody_bdy_uc_tbl_s_from_date1").send_keys(str(ToDate))
driver.find_element_by_css_selector("input[placeholder='Course']").send_keys(str(Coursecode))
time.sleep(4)
driver.find_element_by_css_selector("input[placeholder='Course']").send_keys(Keys.ENTER)
driver.find_element_by_id("m_m_cBody_bdy_uc_tbl_s_btnSearch").click()
time.sleep(4)

courseID="//a[contains(., 'Details') and contains(@href,"+str(ID)+")]"

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,courseID))).click()
select = Select(driver.find_element_by_name('m$m$cBody$bdy$uc$tbl$p$e$location_en'))
select.select_by_visible_text(str(Loction))
time.sleep(10)

s = driver.find_element_by_xpath("//*[text()='Save']")
# perform click with execute_script method
driver.execute_script("arguments[0].click();",s)
print("Page title after click: " + driver.title)
time.sleep(10)

从这个问题你必须使用openpyxl模块

下面的代码将帮助您解决问题

import openpyxl

path1 = r'C:\Users \Desktop\Project\NEWDATA.xlsx'
wb1 = openpyxl.load_workbook(path1) # this will open your excel file
ws1 = wb1.worksheets[0] # this will open your excel sheet1
LastRowCount = ws1.max_row # this will find max row from the excel
start_row_number = 2

for i in range(int(start_row_number), int(LastRowCount)):

    # it will get data of row i which will increase upto maxrow and columnnumber which is fix in excel file of FromDateColumnNumber
    Fromdate = ws1.cell(row=i, column=int(FromdateColumnNumber).value
    driver.find_element_by_id(
        "m_m_cBody_bdy_uc_tbl_s_from_date").send_keys(str(Fromdate))
    ToDate=ws1.cell(row=i, column=int(ToDateColumnNumber).value
    driver.find_element_by_id(
        "m_m_cBody_bdy_uc_tbl_s_from_date1").send_keys(str(ToDate))
    Coursecode=ws1.cell(row=i, column=int(CoursecodeColumnNumber).value
    driver.find_element_by_css_selector(
        "input[placeholder='Course']").send_keys(str(Coursecode))
    time.sleep(4)
    driver.find_element_by_css_selector(
        "input[placeholder='Course']").send_keys(Keys.ENTER)
    driver.find_element_by_id("m_m_cBody_bdy_uc_tbl_s_btnSearch").click()
    time.sleep(4)
    Loction=ws1.cell(row=i, column=int(LoctionColumnNumber).value
    courseID="//a[contains(., 'Details') and contains(@href,"+str(ID)+")]"

    WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, courseID))).click()
    select=Select(driver.find_element_by_name(
        'm$m$cBody$bdy$uc$tbl$p$e$location_en'))
    select.select_by_visible_text(str(Loction))
    time.sleep(10)

    s=driver.find_element_by_xpath("//*[text()='Save']")
    # perform click with execute_script method
    driver.execute_script("arguments[0].click();", s)
    print("Page title after click: " + driver.title)
    time.sleep(10)

希望这会有所帮助