from selenium import webdriver
import unittest
import time
import traceback
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.common.exceptions import TimeoutException,NoSuchElementException
class testDemo(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(executable_path="驱动路径")
def test_uploadFile(self):
url = ''
self.driver.get(url)
try:
wait = WebDriverWait(self.driver,10,0.2)
wait.until(EC.element_to_be_clickable((By.ID,'file')))
except TimeoutException:
print(traceback.print_exc())
except NoSuchElementException:
print(traceback.print_exc())
except Exception:
print(traceback.print_exc())
else:
fileBox = self.driver.find_element_by_id("file_id")
fileBox.send_keys(r"c\\test.txt") #要上传文件的路径
time.sleep(4)
#暂停,查看是否正常运行
submit = self.driver.find_element_by_id("submit")
submit.click()
#文件上传需要时间,可以添加显式等待上传成功
#通过title_is判断成功后的页面标题,符合期望,执行后续的代码
wait.until(EC.title_is("文件上传成功"))
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
selenium实现文件上传(python)
最新推荐文章于 2024-06-10 15:00:00 发布