使用Selenium从Python中的命令行输入上载文件

时间:2017-03-19 02:03:40

标签: python selenium

  1 
  2 import sys
  3 from selenium import webdriver
  4 from selenium.webdriver.common.keys import Keys
  5 from selenium.webdriver.common.alert import Alert
  6 from selenium.webdriver.support.ui import Select
  7 
  8 email = sys.argv[1]
  9 file = sys.argv[2]
 10 
 11 print "You're file is " + file
 12 print "You're email is " + email
 13 
 14 driver = webdriver.Firefox("/usr/local/bin")
 15 driver._is_remote = False
 16
 17 driver.get("https://www.ngs.noaa.gov/OPUS/")
 18 assert "OPUS" in driver.title
 19 
 20 upload = driver.find_element_by_name("uploadfile")
 21 
 22 upload.send_keys(file)
 23 
 24 dropdown = driver.find_element_by_name('ant_type')
 25 select = Select(dropdown)
 26 select.select_by_index(591)
 27 
 28 email = driver.find_element_by_name("email_address")
 29 email.send_keys(email)
 30 
 31 driver.find_element_by_name("Static").click()
 32 
 33 #Alert(driver).accept()
 34 
 35 #sys.exit()

我将文件放在同一目录中但是当我运行时出现此错误:

selenium.common.exceptions.WebDriverException:消息:找不到文件:uhc30010.15d

但它确实在第11行打印时打印出正确的文件,因此我不确定为什么它无法找到该文件

1 个答案:

答案 0 :(得分:0)

您需要发送完整路径,例如:

upload.send_keys(os.path.join(os.getcwd(), file))