简体   繁体   中英

How Do I Import an Excel File in Python?

I am a beginner so please excuse me if my coding is a little excessive. I am trying to import an .xlsx file and use the fields in the spreadsheet in place of the 'email', 'password', 'first name','last name', and 'city' that are on the script. I also want the script to loop until all of the fields are properly input into the script.

Here is a picture of the spreadsheet I'd like to use:

在此输入图像描述

Here is the script:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common import action_chains, keys
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.common.exceptions import ElementNotVisibleException
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import time
from random import randint
from multiprocessing import Process
from threading import Thread
import threading
import time, sys, os


root_url='https://www.nike.com/us/en_us/p/settings'

url='https://www.nike.com/us/en_us/p/settings'


driver = webdriver.Chrome('/Users/mileswalker/chromedriver')

driver.set_window_position(0, 0)

driver.set_window_size(500, 500)



driver.maximize_window()
driver.implicitly_wait(0.5)
driver.get(url)
time.sleep(0.5)
wait = WebDriverWait(driver, 10)



#Email
driver.find_element_by_name('emailAddress').send_keys("johndoe@example.com")

print "Successfully Entered Email..."   


#Password

driver.find_element_by_xpath("//input[@placeholder='Password']").send_keys("Example") 

print "Successfully Entered Password..."    

#Login Button

driver.find_element_by_xpath("//input[@value='LOG IN']").click()

time.sleep(10.0)


#First Name
driver.find_element_by_xpath('//*[@id="first-name"]').clear()
driver.find_element_by_xpath('//*[@id="first-name"]').send_keys("John")

print "Successfully Entered First Name..."

#Last Name
driver.find_element_by_xpath('//*[@id="last-name"]').clear()
driver.find_element_by_xpath('//*[@id="last-name"]').send_keys("Doe")

print "Successfully Entered Last Name..."


#City
driver.find_element_by_xpath('//*[@id="town"]').clear()
driver.find_element_by_xpath('//*[@id="town"]').send_keys("District Heights")

print "Successfully Entered City..."

time.sleep(2.5)

#Save Button
driver.find_element_by_xpath('//*[@id="content"]/div[1]/div[2]/div[1]/form/div[18]/button[2]').click()

print "Successfully Saved Account Information..."

time.sleep(2.5)

#Logout

driver.find_element_by_xpath('/html/body/div[8]/nav/div[1]/ul[2]/li[1]').click()
driver.find_element_by_xpath('//*[@id="exp-profile-dropdown"]/ul/li[9]').click()

print "Successfully Logged Out..."
print "Moving On To The Next Nike Account."

driver.close()

I prefer pyopenxl library. It is easy to read/write excel files with it.

https://openpyxl.readthedocs.io/en/default/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM