使用send_keys传文件路径
#encoding=utf-8
from selenium import webdriver
import unittest
import time
import traceback
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException,NoSuchElementException
class TestDemo(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome(executable_path="d:\\driver\\chromedriver")
def test_uploadFileBySendKeys(self):
url= "http://127.0.0.1/test_upload_file.html"
self.driver.get(url)
try:
wait=WebDriverWait(self.driver,10,0.2)
wait.until(EC.element_to_be_clickable((By.ID,'file')))
except TimeoutException as e:
# 捕获TimeoutException异常
print (traceback.print_exc())
except NoSuchElementException as e:
# 捕获NoSuchElementException异常
print (traceback.print_exc())
except Exception as e:
# 捕获其他异常
print (traceback.print_exc())
else:
fileBox=self.driver.find_element_by_id('file')
fileBox.send_keys("e:\\a.py")
time.sleep(3)
fileSubmitButton=self.driver.find_element_by_id("filesubmit")
fileSubmitButton.click()
#wait.until(EC.title_is(u"文件上传成功"))
time.sleep(3)
def tearDown(self):
self.driver.quit()
if name == ‘main’:
unittest.main()