2017-10-06 91 views
0

我已經編寫了一個使用Selenium的Python腳本來自動將原始GPS數據文件上傳到OPUS [https://www.ngs.noaa.gov/OPUS/]用於我的研究。我有一個完美的Windows版本,現在正試圖在Linux/Ubuntu 16.04計算機上工作。不幸的是,當我試圖將文件上傳到OPUS網站時,我總是收到錯誤信息。我的代碼如下:在Ubuntu上使用Python Selenium上傳文件

import os 
import time 
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 

directory = 'Documents/UNAVCO/HCC1/Y13' # Directory of GPS data files 

# Loop through all files within the specified directory 
for file in os.listdir(directory): 

driver = webdriver.Chrome() # Open Chrome Driver 
driver.get('https://www.ngs.noaa.gov/OPUS/') # Navigate to OPUS 
website 
time.sleep(5) # Wait 5 seconds 

full_dir = os.path.join(directory,file) 
print full_dir 
file_upload = driver.find_element_by_name('uploadfile') 
file_upload.send_keys(full_dir) 

ID = 'TRM55970.00' # ID of GPS atenna 
antenna_type = 
driver.find_element_by_xpath("//option[contains(text(),'%s')]"%ID)   
antenna_type.click() # Select the option 

h = driver.find_element_by_name('height') #Find height element from 

h.clear() # Clear element 
h.send_keys('2.00') # Set value of height element 

email = driver.find_element_by_name('email_address') # Find email 
element from HTML 
email.send_keys('[email protected]') # Set email element to 
recipient 

submit = driver.find_element_by_name('Static').click() # Submit 
current data file 
time.sleep(1) 

os.remove(full_dir) # Delete file 
driver.close() # Close the browser 

print(file + ' ' + 'uploaded') # Visual of files uploaded 

我收到以下錯誤:

enter cTraceback (most recent call last): 
File "/home/zacparra/OPUSpush.py", line 24, in <module> 
file_upload.send_keys(full_dir) 
File "/usr/local/lib/python2.7/dist- 
packages/selenium/webdriver/remote/webelement.py", line 352, in 
send_keys 
'value': keys_to_typing(value)}) 
File "/usr/local/lib/python2.7/dist- 
packages/selenium/webdriver/remote/webelement.py", line 501, in 
_execute 
return self._parent.execute(command, params) 
File "/usr/local/lib/python2.7/dist- 
packages/selenium/webdriver/remote/webdriver.py", line 308, in execute 
self.error_handler.check_response(response) 
File "/usr/local/lib/python2.7/dist- 
packages/selenium/webdriver/remote/errorhandler.py", line 194, in 
check_response 
raise exception_class(message, screen, stacktrace) 
WebDriverException: Message: unknown error: path is not absolute: 
Documents/UNAVCO/HCC1/Y13/hcc11750.13d 
(Session info: chrome=61.0.3163.100) 
(Driver info: chromedriver=2.26.436382 
(70eb799287ce4c2208441fc057053a5b07ceabac),platform=Linux 4.10.0-35- 
generic x86_64)ode here 

如前所述,代碼稍加修改版本的作品完美的Windows操作系統。我已經尋找上傳文件的其他方法,但還沒有找到適當的解決方案來解決這個問題。任何幫助將不勝感激。

回答

0

directory變量的值爲Documents/UNAVCO/HCC1/Y13,它表示相對路徑。在python硒中,你需要提供絕對路徑的文件或目錄。

因此請將directory初始化爲絕對路徑。這應該解決問題。

+0

它現在完美運作。剛剛錯過了一個微妙的細節。非常感謝! –

+0

如果能幫到您,請您接受答案嗎? – Shubhangi

相關問題