Home > Back-end >  Python Code wont loop through website with selenium
Python Code wont loop through website with selenium

Time:04-10

i have a Excel sheet with article numbers and want to loop through each row in the excel spreadsheat and search the product price on a website with selenium and afterwards write the product price to the same spreadsheet but somehow the loop doenst work

Hopefully u can help me

'''

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[@])[1]' ).text


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

indx = indx   1

'''

CodePudding user response:

From this problem you have to use openpyxl module

Below code will help you to solve your problem

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[@])[1]').text

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

Hope this will Help

  • Related