2017-06-30 64 views
2

我在Python中使用Selenium來創建自動測試。在這個測試中,我試圖從本地目錄中選擇一個文件。我能夠找到一個使用Java的參考,但我努力將其轉換爲Python。 https://sqa.stackexchange.com/questions/12851/how-can-i-work-with-file-uploads-during-a-webdriver-test使用Selenium選擇本地文件自動測試

element=driver.find_element_by_id("file_browse").click() 
    driver.file_detector("<>") 
    upload=driver.find_element_by_id("<>") 
    keys=upload.send_keys("<>") 

對於文件檢測功能我不斷收到該對象是不可調用的。應該輸入什麼內容?

謝謝!

+0

請添加完整的回溯。哪些對象不可調用? [mcve] –

+0

我得到'localfiledetector'對象不可調用。我目前有「探測器」作爲driver.file_detector() – kvdesai2

+0

輸入您期望'driver.file_detector()'做什麼?我對api不熟悉,但它似乎是一個不可調用的類實例。你確定你想要做什麼需要使用「文件檢測器」嗎?看看這個答案。 https://stackoverflow.com/a/10472542/1977847 –

回答

1

只是刪除這一行:

driver.file_detector("<>") 

Python的遠程webdriver的默認使用LocalFileDetector()。這似乎是你想要的,看着鏈接的Java示例。

如果需要覆蓋默認設置,你可以使用或繼承現有的文件探測器中的一個從selenium.webdriver.remote.file_detector

似乎沒有要如何使用FileDetector任何文件,但source code is quite short and straightforward.

from selenium.webdriver.remote.file_detector import UselessFileDetector 

driver.file_detector = UselessFileDetector() 

用於設置對象成員的Python的ideom只是簡單地使用賦值運算符(=)而不是調用set方法,就像在Java中一樣。

相關問題