我是 Selenium 的新手,我有一个脚本可以将文件上传到服务器。
用ide版本来说,它会上传文件,但是当我将测试用例导出为python 2/unittest/webdriver时,它不会上传文件。
它不会给我任何错误,只是不会上传它...
python脚本是:
driver.find_element_by_id("start-upload-button-single").click()
driver.find_element_by_css_selector("input[type=\"file\"]").clear()
driver.find_element_by_css_selector("input[type=\"file\"]").send_keys("C:\\\\Documents and Settings\\\\pcname\\\\Desktop\\\\ffdlt\\\\test.jpeg")
我搜索了解决方案,但除了将其与AutoIt或AutoHotKey集成外,没有找到其他解决方案。
第一行打开Firefox的“文件上传”框。
最佳答案
您的代码非常适合我(我使用Firefox,Chrome驱动程序对其进行了测试)
我猜想的一件事是过多的反斜杠(\
)转义。
请尝试以下操作:
driver.find_element_by_id("start-upload-button-single").click()
driver.find_element_by_css_selector('input[type="file"]').clear()
driver.find_element_by_css_selector('input[type="file"]').send_keys("C:\\Documents and Settings\\pcname\\Desktop\\ffdlt\\test.jpeg")
或者
driver.find_element_by_id("start-upload-button-single").click()
driver.find_element_by_css_selector('input[type="file"]').clear()
driver.find_element_by_css_selector('input[type="file"]').send_keys(r"C:\Documents and Settings\pcname\Desktop\ffdlt\test.jpeg")
关于python - Selenium webdriver上传文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18823139/