在Ubuntu上用Python Selenium上传文件

2024-06-17 01:57:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个Python脚本,利用Selenium自动将原始GPS数据文件上传到OPUS[https://www.ngs.noaa.gov/OPUS/]进行研究。我有一个完美的Windows版本,现在正试图让它在Linux/ubuntu16.04计算机上运行。不幸的是,当我试图上传一个文件到OPUS网站时,我总是得到一个错误。我的代码如下:

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('zacpopus@gmail.com') # 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

我收到以下错误:

^{pr2}$

如前所述,稍微修改的代码版本在Windows操作系统上运行得非常完美。我一直在寻找上传文件的替代方法,但没有找到一个合适的解决方案来解决这个问题。任何帮助都将不胜感激。在


Tags: offromimportbyosemaildriverdir
1条回答
网友
1楼 · 发布于 2024-06-17 01:57:34

directory变量的值为Documents/UNAVCO/HCC1/Y13,表示相对路径。在pythonselenium中,您需要提供文件或目录的绝对路径。在

因此,请将directory变量初始化为绝对路径。这应该可以解决这个问题。在

相关问题 更多 >