2017-03-25 212 views
1

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

这里是电子表格的图片我想使用:

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("[email protected]") 

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

有一个在这里阅读:http://stackoverflow.com/questions/22169325/read-excel-file-in-python。应该很容易更改代码以适应您的需求 – BioGenX

+1

有几个包可以处理xlsx文件,网络搜索和一些教程将是一个好的开始。或者,您可以将表格保存为CSV文件,并使用python的标准'csv'模块或甚至'pandas'模块。很多选择! – tdelaney

+0

我同意@tdelaney,在你的情况下,CSV将是一个不错的选择。 –

回答