如何在Python中导入Excel文件?

时间:2017-03-25 04:25:07

标签: python selenium

我是初学者所以请原谅我,如果我的编码有点过分。我正在尝试导入.xlsx文件并使用电子表格中的字段代替脚本上的“电子邮件”,“密码”,“名字”,“姓氏”和“城市”。我还希望脚本循环,直到所有字段都正确输入到脚本中。

以下是我要使用的电子表格的图片:

enter image description here

这是脚本:

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()

1 个答案:

答案 0 :(得分:0)

我更喜欢pyopenxl库。使用它可以轻松读取/写入excel文件。

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