Python 代码不会在带有 selenium 的网站上循环

Python Code wont loop through website with selenium

我有一个带有文章编号的 Excel sheet 并且想遍历 excel spreadsheat 中的每一行并在带有 selenium 的网站上搜索产品价格然后写产品价格与价差相同sheet 但不知为何循环有效

希望你能帮助我

'''

lastrow= 10
indx = 2
Artikelnumber = sheet.cell(row=indx, column=2).value

while indx > lastrow:

#Search
search_txt = driver.find_element(By.ID, 'suggestSearch')
search_txt.click()

#Number
search_txt.send_keys(Artikelnumber)
search_txt.send_keys(Keys.ENTER)
#Produktpreis
Produktpreis = driver.find_element(By.XPATH, '(//span[@class="priceValue  "])[1]' ).text


workbook = load_workbook(filename='test.xlsx')
sheet = workbook["test"]
sheet.cell(row=indx, column=8).value = Produktpreis

indx = indx + 1

'''

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

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

import openpyxl

path1 = r'Excel Path'
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) + 1):

    # it will get data of row i which will increase upto maxrow and columnnumber which is fix in excel file of FromDateColumnNumber
    Artikelnumber = ws1.cell(row=i, column=2).value

    # Search
    search_txt = driver.find_element(By.ID, 'suggestSearch')
    search_txt.click()

    # Number
    search_txt.send_keys(Artikelnumber)
    search_txt.send_keys(Keys.ENTER)
    # Produktpreis
    Produktpreis = driver.find_element(
        By.XPATH, '(//span[@class="priceValue  "])[1]').text

    workbook = load_workbook(filename='Excel Path')
    sheet = workbook["test"]
    sheet.cell(row=i, column=8).value = Produktpreis
sheet.save()

希望这会有所帮助