So one tip about openpyxl and Excel is that you need to make absolutely sure there is no extraneous data or else openpyxl will read that in when you don't want it to do so. So use clear contents in all cells around the data you want to read in.
Here is my suggested edit with comments so you can see what I am doing.
def mac_attack():
from openpyxl import load_workbook
# set file path
filepath="C:\Users\myFile.xlsx"
wb = load_workbook(filepath)
sheet = wb["Sheet1"]
# Read In the Entire Column of Data, not Just One Value
sourceIPCol = sheet['A']
# This Should Give you an Immutable 'Tuple' Data Type
# You can confirm that by print(type(sourceIPCol))
# Assuming I am right, Convert it to a List next
sourceIPCol = list(sourceIPCol)
# Iterate Through Each Address in your list of IP addresses
# Note that you may prefer to have this as a function above this
# function which you 'call' from this function. It may be cleaner.
B2row = 0 # Keep track of which row you want to print to in B2
for address in sourceIPCol:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
driverPath = "C:\Users\chromedriver.exe"
browser = webdriver.Chrome(executable_path=driverPath, options=chrome_options)
browser.get(ip)
browser.implicitly_wait(10)
mac = browser.find_elements_by_xpath('/html/body/div[2]/table[2]/tbody/tr[1]/td[2]')[0].text
#Write Corresponding Value to Column B in SAME SHEET
sheet[B2row]["B"].value = mac
# NOTE THAT SO LONG AS YOU ITERATE THROUGH THE ENTIRE COLUMN
# YOU SHOULDN'T NEED TO SAVE UNTIL YOU GET THROUGH THE WHOLE
# LIST, BUT I LEFT THE LINE BELOW HERE AS I DON'T THINK IT HURTS ANYTHING. IT WILL JUST MAKE YOUR CODE SLOWER. TECHNICALLY YOU JUST NEED TO SAVE ONCE AT THE END BEFORE YOU CLOSE THE FILE STREAM.
wb.save("C:\Users\myFile.xlsx")
print(mac)
print(ip)
B2row = B2row + 1
mac_attack()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…