Python 代码不会使用 selenium 循环浏览网站

发布于 2025-01-20 05:08:32 字数 678 浏览 0 评论 0原文

我有一个带有商品编号的 Excel 工作表,想要循环遍历 Excel 电子表格中的每一行,并使用 selenium 在网站上搜索产品价格,然后将产品价格写入同一个电子表格中,但不知何故,循环不起作用

希望你能帮助我

'''

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

'''

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


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

indx = indx + 1

'''

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚然 2025-01-27 05:08:32

从这个问题你必须使用 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()

希望这会有所帮助

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[@class="priceValue  "])[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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文