So i have this problem that when i retrieve information from a website using Selenium Python to Excel, So the steps are 1
, I take information from Excel from each row
and paste in the input
in the website and click submit Button
to get information. 2
I load the website and retrieve back the information to the same row
to different column. However, the information i retrieve went to row + 1
which is below the row that i expect information from. Any help? Below is my codes for selenium.
for r in range(4, rows+1):
driver.implicitly_wait(15)
po = XLUtlis.readData(path, 'BO', r, 2)
partNo = XLUtlis.readData(path, 'BO', r, 4)
pso = XLUtlis.readData(path, 'BO', r, 3)
driver.implicitly_wait(15)
driver.find_element_by_xpath('//*[@placeholder="Search with customer code"]').send_keys('J199')
driver.find_element_by_xpath('//*[@placeholder="Search with customer ref number"]').send_keys(po)
driver.find_element_by_xpath('//*[@placeholder="Search with part number"]').send_keys(partNo)
button = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/div[1]/div/div[2]/div/section/div/div/div[6]/div/div/button[1]')
driver.execute_script("arguments[0].click();", button)
try:
XLUtlis.writeData(path, 'BO', r, 26, 'done')
remark = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/div[1]/div/div[2]/div/section/div/div/div[7]/div/div/lightning-datatable/div[2]/div/div/div/table/tbody/tr/td[15]/lightning-primitive-cell-factory/lightning-primitive-cell-wrapper/div/slot/lightning-primitive-cell-types/lightning-formatted-text').text
XLUtlis.writeData(path, 'BO', r, 24, remark)
case = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/div[1]/div/div[2]/div/section/div/div/div[7]/div/div/lightning-datatable/div[2]/div/div/div/table/tbody/tr/td[20]/lightning-primitive-cell-factory/lightning-primitive-cell-wrapper/div/slot/lightning-primitive-cell-types/lightning-primitive-cell-button/lightning-button/button').text
XLUtlis.writeData(path, 'BO', r, 25, case)
ESD = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/div[1]/div/div[2]/div/section/div/div/div[7]/div/div/lightning-datatable/div[2]/div/div/div/table/tbody/tr/td[11]/lightning-primitive-cell-factory/lightning-primitive-cell-wrapper/div/slot/lightning-primitive-cell-types/lightning-formatted-date-time').text
XLUtlis.writeData(path, 'BO', r, 23, ESD)
Here is the code for me to connect to Excel.
import openpyxl
def getRowCount(file, sheetName):
workbook = openpyxl.load_workbook(file)
sheet = workbook.get_sheet_by_name(sheetName)
return (sheet.max_row)
def getColumnCount(file,sheetName):
workbook = openpyxl.load_workbook(file)
sheet = workbook.get_sheet_by_name(sheetName)
return(sheet.max_column)
def readData(file, sheetName, rownum, columno):
workbook = openpyxl.load_workbook(file)
sheet = workbook.get_sheet_by_name(sheetName)
return sheet.cell(row=rownum, column=columno).value
def writeData (file, sheetName, rownum, columno, data):
workbook = openpyxl.load_workbook(file,keep_vba=True,read_only=False)
sheet = workbook.get_sheet_by_name(sheetName)
sheet.cell(row=rownum, column=columno).value = data
workbook.save(file)