2017-03-25 115 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將是一個不錯的選擇。 –

回答