Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88087

How to: Looping code for each excel row openpyxl?

$
0
0

I am using python, selenium, openpyxl in order to fill a form online. To fill the form I am taking values from specific cells on excel (.xlsx). With the following code I can insert to the online form the data I need to take:

From the cell A2 I take the NAME of the person and insert in to the online form and from cell B2 I take the AGE of the person and insert in to the online form then I click send as a draft.

I would like to create a loop in which the code will start again from driver.get("https://XXXXXXXXXX") to go again to the page where I need to fill the form, but this time I would like to take:

From the cell A3 I take the NAME of the person and insert in to the online form and from cell B3 I take the AGE of the person and insert in to the online form then I click send as a draft.

Then again another loop from driver.get("https://XXXXXXXXXX") to insert the data from the row 4 and so on until the row on excel is empty.

from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.common.exceptions import NoSuchElementException
import openpyxl

driver: WebDriver = 
webdriver.Chrome("/Users/HHHHH/PycharmProjects/excel/driver/chromedriver")

driver.maximize_window()

excel_document = openpyxl.load_workbook(r"/Users/HHHHH/Desktop/testtesttest1.xlsx", 
data_only=True)

sheet = excel_document["Sheet1"]



driver.get("https://XXXXXXXXXX")

#Insert in the form the Name of the person

prevsymbol = sheet["A2"].value
if prevsymbol == None:
    pass
else:
    try:
        driver.find_element_by_id("name").send_keys(sheet["A2"].value)
    except NoSuchElementException:
        print("A2:(name) Not Found")

#Insert in the form the Age of the person

prevsymbol = sheet["B2"].value
if prevsymbol == None:
    pass
else:
    try:
        driver.find_element_by_id("age").send_keys(sheet["B2"].value)
    except NoSuchElementException:
        print("B2:(Age) Not Found")

#click Save as a draft

driver.find_element_by_xpath("xpath_save_as_draft"). click()

Viewing all articles
Browse latest Browse all 88087

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>